WPF 将窗口标题绑定到属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9603803/
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 bind title of window to property
提问by lace.john
I am trying to bind the value of a property (MyTitle) of a class (MainWindow) that derives from Window. I have created a dependency property called MyTitleProperty, implemented the INotifyPropertyChanged interface and modified the set method of MyTitle to call the PropertyChanged event, passing "MyTitle" as the property name parameters. I set MyTitle to "Title" in the constructor but when the window opens the title is blank. If I put a break point on the Loaded event then MyTitle = "Title" but this.Title = "". This is surely something unbelievably obvious that I've not noticed. Please help!
我正在尝试绑定从 Window 派生的类 (MainWindow) 的属性 (MyTitle) 的值。我创建了一个名为 MyTitleProperty 的依赖属性,实现了 INotifyPropertyChanged 接口并修改了 MyTitle 的 set 方法来调用 PropertyChanged 事件,将“MyTitle”作为属性名称参数传递。我在构造函数中将 MyTitle 设置为“Title”,但是当窗口打开时,标题为空白。如果我在 Loaded 事件上放置一个断点,则 MyTitle = "Title" 但 this.Title = ""。这肯定是我没有注意到的令人难以置信的明显事情。请帮忙!
MainWindow.xaml
主窗口.xaml
<Window
x:Class="WindowTitleBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:WindowTitleBindingTest"
Height="350"
Width="525"
Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}}"
Loaded="Window_Loaded">
<Grid>
</Grid>
</Window>
MainWindow.xaml.cs:
主窗口.xaml.cs:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public static readonly DependencyProperty MyTitleProperty = DependencyProperty.Register("MyTitle", typeof(String), typeof(MainWindow));
public String MyTitle
{
get { return (String)GetValue(MainWindow.MyTitleProperty); }
set
{
SetValue(MainWindow.MyTitleProperty, value);
OnPropertyChanged("MyTitle");
}
}
public MainWindow()
{
InitializeComponent();
MyTitle = "Title";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
}
回答by Scroog1
public MainWindow()
{
InitializeComponent();
DataContext = this;
MyTitle = "Title";
}
Then you just need in the XAML
然后你只需要在 XAML 中
Title="{Binding MyTitle}"
Then you don't need the dependency property.
那么你不需要依赖属性。
回答by Louis Kottmann
First off, you don't need INotifyPropertyChanged
if you just want to bind to a DependencyProperty
. that would be redundant.
首先,INotifyPropertyChanged
如果您只想绑定到DependencyProperty
. 那将是多余的。
You don't need to set DataContext
either, that's for a ViewModel scenario. (look into the MVVM pattern whenever you get a chance).
您也不需要设置DataContext
,这是针对 ViewModel 场景的。(只要有机会,就查看 MVVM 模式)。
Now your declaration of dependency property is incorrect, it should be:
现在你的依赖属性声明不正确,它应该是:
public string MyTitle
{
get { return (string)GetValue(MyTitleProperty); }
set { SetValue(MyTitleProperty, value); }
}
// Using a DependencyProperty as the backing store for MyTitle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyTitleProperty =
DependencyProperty.Register("MyTitle", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));
Notice the UIPropertyMetadata
: it sets the default value for your DP.
请注意UIPropertyMetadata
:它为您的 DP 设置默认值。
And lastly, in your XAML:
最后,在您的 XAML 中:
<Window ...
Title="{Binding MyTitle, RelativeSource={RelativeSource Mode=Self}}"
... />
回答by Jonas
Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=Self}}"