C# WPF:将标签绑定到类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2317229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: Binding a Label to a class property
提问by Gareth
I'm trying to get the content of a label to bind to the string property of a class instance without much success.
我试图获取标签的内容以绑定到类实例的字符串属性,但没有取得多大成功。
XAML:
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#:
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; }
}
}
i.e. when I click the button, I set the properties of MyFoo to "Hello" and "Dave", and want that reflected in the labels on the UI. I've set the Content as a binding but something isn't right. What am I doing wrong here?
即当我单击按钮时,我将 MyFoo 的属性设置为“Hello”和“Dave”,并希望将其反映在 UI 上的标签中。我已将内容设置为绑定,但有些不对劲。我在这里做错了什么?
采纳答案by Vlad
You may make your MyFoo
a dependency property and set the DataContext
to your Window1
instance:
您可以创建MyFoo
一个依赖属性并将其设置DataContext
为您的Window1
实例:
<Window DataContext="{Binding RelativeSource={RelativeSource Self}}" ...>
See this articlefor more details.
有关更多详细信息,请参阅此文章。
Making MyFoo
a dependency property is not compulsory. it may work with just a property if you set the property value beforeassigning the DataContext
. (But never with a field.) However if you want the labels to pick up the changing values of W1
and W2
(or you don't know/care if the values are set before or after assigning the DataContect
), you need Foo
to be either a DependencyObject
, or implement interface INotifyPropertyChanged
.
制作MyFoo
依赖属性不是强制性的。如果您在分配DataContext
. (但永远不要使用字段。)但是,如果您希望标签获取W1
和的变化值W2
(或者您不知道/关心这些值是在分配 之前还是之后设置的DataContect
),则您需要Foo
成为DependencyObject
,或实现接口INotifyPropertyChanged
。
回答by Arcturus
Or give your Window a name: like NameOfWindow
and use a ElementName binding:
或者给你的 Window 一个名字:喜欢NameOfWindow
并使用一个 ElementName 绑定:
Content="{Binding ElementName=NameOfWindow, Path=MyFoo.W1}"
Complete sample XAML:
完整的示例 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>