7

私がやろうとしているのは、パブリックプロパティを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>

4

3 に答える 3

15

データコンテキストを追加してプロパティにアクセスするだけです

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

于 2011-01-19T20:39:54.577 に答える
7

最初に、 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>
于 2011-01-19T20:22:35.677 に答える
1

実際には INotifyPropertyChanged を実装する必要はありません。ただし、これは 1 回限りのデータ バインディングになります。

たとえば、XAML では次のようになります。

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

コード内:

    public string SomeProp { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        SomeProp = "Test Test Test";
        SomeTextBlock.DataContext = this;          
    }
于 2013-12-11T22:01:35.390 に答える