将 Window 的标题绑定到文本
Binding Title of Window to text
我有一个具有 "title" 属性 的视图模型,我的数据上下文设置为此虚拟机。
我有一个 TextBox
需要显示 window 的标题,当我在后面的“.cs”文件中更改它时需要更改它。
我们如何从“.cs”文件而不是 viemodel 中绑定 属性 中的 window 的标题?
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Left"
Text="{Binding Title,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"
Margin="10,8,0,0"/>
我正在从 MSDN example
中取样
试试这个:
<Window ... Title="{Binding TitleProperty, RelativeSource={RelativeSource Self}}"
如果您打算使用 TextBox
:
更改标题,code-behind class 应该实现 INotifyPropertyChanged
接口
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="{Binding MyTitle, RelativeSource={RelativeSource Self}}" Height="300" Width="300">
<StackPanel>
<TextBox Text="{Binding MyTitle, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=Window}}" />
</StackPanel>
</Window>
public partial class Window1 : Window, INotifyPropertyChanged
{
public Window1()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _title;
public string MyTitle
{
get { return _title; }
set { _title = value; NotifyPropertyChanged(); }
}
}
我有一个具有 "title" 属性 的视图模型,我的数据上下文设置为此虚拟机。
我有一个 TextBox
需要显示 window 的标题,当我在后面的“.cs”文件中更改它时需要更改它。
我们如何从“.cs”文件而不是 viemodel 中绑定 属性 中的 window 的标题?
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Left"
Text="{Binding Title,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"
Margin="10,8,0,0"/>
我正在从 MSDN example
中取样试试这个:
<Window ... Title="{Binding TitleProperty, RelativeSource={RelativeSource Self}}"
如果您打算使用 TextBox
:
INotifyPropertyChanged
接口
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="{Binding MyTitle, RelativeSource={RelativeSource Self}}" Height="300" Width="300">
<StackPanel>
<TextBox Text="{Binding MyTitle, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=Window}}" />
</StackPanel>
</Window>
public partial class Window1 : Window, INotifyPropertyChanged
{
public Window1()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _title;
public string MyTitle
{
get { return _title; }
set { _title = value; NotifyPropertyChanged(); }
}
}