【问题标题】:How to databind public property in xaml如何在 xaml 中对公共属性进行数据绑定
【发布时间】:2011-06-12 00:45:15
【问题描述】:

我要做的就是将一个公共属性绑定到一个 textBlock。我在这里做错了什么?

namespace WpfApplication1
{

    public partial class MainWindow : Window
    {

        public string test { get; set; }

        public MainWindow()
        {
            test = "this is a test";
            InitializeComponent();
        }
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1"  VerticalAlignment="Top" Text="{Binding Source={StaticResource test}}" />
</Grid>

【问题讨论】:

    标签: wpf xaml public-key


    【解决方案1】:

    您可以简单地添加数据上下文并访问您的属性

    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private string _test;
        public string test
        {
            get
            {
                return _test;
            }
            set
            {
                _test = value;
                OnPropertyChanged("test");
            }
        }
        public MainWindow()
        {
            test = "this is a test";
            InitializeComponent();
            DataContext = this;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(String name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1"  VerticalAlignment="Top" Text="{Binding test}"/>
    

    还可以查看这篇文章了解何时使用 ObjectDataProvider 的详细信息

    http://bea.stollnitz.com/blog/?p=22

    【讨论】:

      【解决方案2】:

      首先你需要你的类来实现INotifyPropertyChanged 或一个属性为DependencyProperty 以在文本框文本更改时更改属性值,

      namespace WpfApplication1
      {
      public partial class MainWindow : Window, INotifyPropertyChanged
      {
          private string _test 
          public string test 
          { 
              get
              {
                 return _test;
              } 
              set
              {
                  _test = value;
                  OnPropertyChanged("test");
              } 
          }
      
          public MainWindow()
          {
              test = "this is a test";
              InitializeComponent();
          }
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(String info)
          {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, new PropertyChangedEventArgs(info));
             }
          }
      }
      
      }
      

      您可以通过为该窗口指定名称并像这样使用 ElementName 属性来绑定到该属性。

      <Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525" Name="myWindow">
      <Window.Resources>
          <ObjectDataProvider x:Key="test"></ObjectDataProvider>
      </Window.Resources>
      <Grid>
          <TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1"  VerticalAlignment="Top" Text="{Binding ElementName=myWindow, Path=test}" />
      </Grid>
      

      【讨论】:

        【解决方案3】:

        您实际上不需要实现 INotifyPropertyChanged。但是,这将是一次性数据绑定。

        例如在 XAML 中:

        <TextBlock Name="SomeTextBlock" Text="{Binding Path=SomeProp}" />
        

        在代码中:

            public string SomeProp { get; set; }
            public MainWindow()
            {
                InitializeComponent();
                SomeProp = "Test Test Test";
                SomeTextBlock.DataContext = this;          
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-15
          • 2011-04-04
          • 2020-03-12
          相关资源
          最近更新 更多