【问题标题】:WPF: Binding a Label to a class propertyWPF:将标签绑定到类属性
【发布时间】:2011-01-20 00:11:16
【问题描述】:

我试图让标签的内容绑定到类实例的字符串属性,但没有取得多大成功。

XAML:

<Window x:Class="WPFBindingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">    
<Grid>        
    <Label Height="28" Margin="12,55,106,0" Name="label1" Background="Bisque"
           Content="{Binding Source=MyFoo, Path=W1}" VerticalAlignment="Top" />

    <Label Height="28" Margin="12,12,106,0" Name="label2" Background="Bisque"
           Content="{Binding Source=MyFoo, Path=W2}"  VerticalAlignment="Top" />

    <Button Height="23" HorizontalAlignment="Right" Margin="0,0,32,48"
            Name="button1" VerticalAlignment="Bottom" Width="89"
            Click="button1_Click">
        Set Properties
    </Button>

</Grid>   
</Window>

C#:

namespace WPFBindingTest
{
   public partial class Window1 : Window
    {
        public Foo MyFoo;

        public Window1()
        {
            InitializeComponent();            

            MyFoo = new Foo();           
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {      
            MyFoo.W1 = "Hello";
            MyFoo.W2 = "Dave";
        }
    }

    public class Foo
    {
        public string W1 { get; set; }
        public string W2 { get; set; }
    }
}

即当我单击按钮时,我将 MyFoo 的属性设置为“Hello”和“Dave”,并希望它们反映在 UI 上的标签中。我已将内容设置为绑定,但有些地方不对。我在这里做错了什么?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您可以将 MyFoo 设置为依赖属性并将 DataContext 设置为您的 Window1 实例:

    <Window DataContext="{Binding RelativeSource={RelativeSource Self}}" ...>
    

    查看article了解更多详情。

    使MyFoo 成为依赖属性不是强制性的。如果您在分配DataContext 之前设置属性值,它可能只适用于一个属性。 (但从不使用字段。)但是,如果您希望标签获取 W1W2 的变化值(或者您不知道/关心这些值是在分配 DataContect 之前还是之后设置的),您需要 FooDependencyObject,或者实现接口 INotifyPropertyChanged

    【讨论】:

    • 谢谢,这为我指明了正确的方向。让 Foo 实现 INotifyPropertyChanged,然后将 Window1 的 DataContext 设置为包含 MyFoo 的 BindingList 的 DataContext。标签内容现在是:{Binding Path=W1, UpdateSourceTrigger=PropertyChanged} 而且它很有效!
    • @Vlad 使用DependencyProperty 和实施INotifyPropertyChanged 有什么区别,还是这本身就是一个问题?
    • @ymw:这是一个不同的问题,实际上相当大。简而言之:两者都适用于绑定,但INotifyPropertyChanged 更轻量级,DependencyProperty 但是如果不使用则不会占用内存,并且可以用于动画、样式、模板、被继承(从父容器到包含元素)等等。参见例如this answer
    • @Vlad 我知道那是 6 年前的事了,但必须给出一个清晰、简洁且更重要的是可以理解的答案的道具。我不知道为什么这么多人努力以清晰简单的方式描述 WPF UI 元素绑定,但您已经做到了!
    • @Redgum:非常感谢!
    【解决方案2】:

    或者给你的窗口一个名字:比如NameOfWindow,并使用一个ElementName绑定:

    Content="{Binding ElementName=NameOfWindow, Path=MyFoo.W1}"
    

    完整的 XAML 示例:

    <Window x:Class="WPFBindingTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Name="NameOfWindow">    
    <Grid>        
        <Label Height="28" Margin="12,55,106,0" Name="label1" Background="Bisque" Content="{Binding ElementName=NameOfWindow, Path=MyFoo.W1}" VerticalAlignment="Top" />
        <Label Height="28" Margin="12,12,106,0" Name="label2" Background="Bisque" Content="{Binding ElementName=NameOfWindow, Path=MyFoo.W2}"  VerticalAlignment="Top" />
        <Button Height="23" HorizontalAlignment="Right" Margin="0,0,32,48" Name="button1" VerticalAlignment="Bottom" Width="89" Click="button1_Click">Set Properties</Button>
    </Grid> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 2018-10-24
      • 2013-04-09
      • 1970-01-01
      相关资源
      最近更新 更多