如何在 xaml 中对公共属性进行数据绑定

发布于 2024-10-13 04:54:12 字数 872 浏览 7 评论 0原文

我想做的就是将公共属性绑定到文本块。我在这里做错了什么?

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>

All I am trying to do is bind a public property to a textBlock. What am I doing wrong here?

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

完美的未来在梦里 2024-10-20 04:54:12

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

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

You can simply add a datacontext and access your property

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}"/>

Also check this post for details of when to use an ObjectDataProvider

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

软糯酥胸 2024-10-20 04:54:12

首先,您需要类来实现 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>

At first you need you class to implement INotifyPropertyChanged or a property to be DependencyProperty for changing property value on textbox text change,

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));
       }
    }
}

}

Than you can bind to that property by giving name to that window, and using ElementName property like this.

<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>
2024-10-20 04:54:12

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

例如在 XAML 中:

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

在代码中:

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

You actually don't need to implement INotifyPropertyChanged. However, this will be a one time data binding.

For example in XAML:

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

In Code:

    public string SomeProp { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        SomeProp = "Test Test Test";
        SomeTextBlock.DataContext = this;          
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文