【问题标题】:WPF bind title of window to propertyWPF 将窗口的标题绑定到属性
【发布时间】:2012-03-07 14:59:07
【问题描述】:

我正在尝试绑定派生自 Window 的类 (MainWindow) 的属性 (MyTitle) 的值。我创建了一个名为 MyTitleProperty 的依赖属性,实现了 INotifyPropertyChanged 接口并修改了 MyTitle 的 set 方法以调用 PropertyChanged 事件,将“MyTitle”作为属性名称参数传递。我在构造函数中将 MyTitle 设置为“Title”,但是当窗口打开时,标题为空白。如果我在 Loaded 事件上设置一个断点,那么 MyTitle = "Title" 但 this.Title = ""。这肯定是令人难以置信的显而易见的事情,我没有注意到。请帮忙!

MainWindow.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:

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

【问题讨论】:

  • 你的 DataContext 是在哪里设置的?
  • 相对 WPF 新手。我以前做过一些绑定,从来没有设置过。我应该设置它吗?我会把它设置成什么?
  • 所以我刚刚快速搜索了一下,似乎添加了 DataContext = this;我的构造函数解决了我的问题。谢谢杰夫!

标签: wpf binding window


【解决方案1】:
public MainWindow()
{
    InitializeComponent();

    DataContext = this;

    MyTitle = "Title";
}

那么你只需要在 XAML 中

Title="{Binding MyTitle}"

那么你就不需要依赖属性了。

【讨论】:

    【解决方案2】:

    首先,如果您只想绑定到DependencyProperty,则不需要INotifyPropertyChanged。那将是多余的。

    您也不需要设置DataContext,这是针对 ViewModel 场景的。 (一有机会就研究一下 MVVM 模式)。

    现在你的依赖属性声明不正确,应该是:

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

    注意UIPropertyMetadata:它为您的 DP 设置默认值。

    最后,在您的 XAML 中:

    <Window ...
           Title="{Binding MyTitle, RelativeSource={RelativeSource Mode=Self}}"
           ... />
    

    【讨论】:

      【解决方案3】:
      Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=Self}}"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        相关资源
        最近更新 更多