Binding to static property

后端 未结 12 2157
夕颜
夕颜 2020-11-22 05:14

I\'m having a hard time binding a simple static string property to a TextBox.

Here\'s the class with the static property:



        
12条回答
  •  一生所求
    2020-11-22 05:39

    These answers are all good if you want to follow good conventions but the OP wanted something simple, which is what I wanted too instead of dealing with GUI design patterns. If all you want to do is have a string in a basic GUI app you can update ad-hoc without anything fancy, you can just access it directly in your C# source.

    Let's say you've got a really basic WPF app MainWindow XAML like this,

    
        
            
            

    That will look something like this:

    In your MainWindow XAML's source, you could have something like this where all we're doing in changing the value directly via textBlock.Text's get/set functionality:

    using System.Windows;
    
    namespace MyWPFApp
    {
        public partial class MainWindow : Window
        {
            public MainWindow() { InitializeComponent(); }
    
            private void Button_Click_Poke_Kilroy(object sender, RoutedEventArgs e)
            {
                textBlock.Text = "              \\|||/\r\n" +
                                 "              (o o) \r\n" +
                                 "----ooO- (_) -Ooo----";
            }
        }
    }
    

    Then when you trigger that click event by clicking the button, voila! Kilroy appears :)

提交回复
热议问题