【问题标题】:Odd Behavior When Setting WPF Window Title from C# Code-behind从 C# 代码隐藏设置 WPF 窗口标题时的奇怪行为
【发布时间】:2019-03-22 20:50:34
【问题描述】:

这里尝试运行为How do I programmatically change the Title in a wpf window? 建议的答案

<Window x:Class="WindowTitle.MainWindow"
     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"
     xmlns:local="clr-namespace:WindowTitle"
     mc:Ignorable="d"
     MinWidth="600" SizeToContent="WidthAndHeight" Title="{Binding MyTitle}">
    <Grid Margin="5">
         <Button Click="Button_Click" Content="Change Title" Width="100"/>
    </Grid>
</Window>

代码隐藏:

using System.Windows;

namespace WindowTitle
{
    public partial class MainWindow : Window
    {
        private int counter = 0;
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            MyTitle = counter.ToString();
            ++counter;
            System.Diagnostics.Debug.Assert(MyTitle != DefaultTitle);
        }
        public const string DefaultTitle = "Default Title";
        public MainWindow() { InitializeComponent(); DataContext = this; }
        public string MyTitle { get; set; } = DefaultTitle;
    }
}

代码在加载时设置标题,但在随后单击按钮时不设置。

有什么想法吗?我被难住了。

【问题讨论】:

    标签: c# wpf titlebar


    【解决方案1】:

    绑定不知道MyTitle 已更改。传达变更的常用方式是实现INotifyPropertyChanged;还可以看看 MVVM,这是通过DataContext 实现它的一种非常常见的方式。

    【讨论】:

      【解决方案2】:

      我以为INotifyPropertyChanged是自动支持的;我错了。这是工作代码:

      <Window x:Class="WindowTitle.MainWindow"
                  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"
                  xmlns:local="clr-namespace:WindowTitle"
                  mc:Ignorable="d"
                  MinWidth="400"
                  SizeToContent="WidthAndHeight"
                  Title="{Binding MainWindowTitle}">
          <Grid Margin="5">
              <Button Click="Button_Click"
                          Content="Change by Binding"
                          HorizontalAlignment="Left"
                          Padding="5,0"/>
              <Button x:Name="ChangeBtn"
                          Click="ChangeBtn_Click"
                          Content="Change by Name"
                          HorizontalAlignment="Right"
                          Padding="5,0"/>
          </Grid>
      </Window>
      using System.Windows;
      namespace WindowTitle {
          public partial class MainWindow : Window, System.ComponentModel.INotifyPropertyChanged {
              public void Button_Click(object sender, RoutedEventArgs e) {
                  MainWindowTitle = titleCount.ToString();
                  ++titleCount;
              }
              void ChangeBtn_Click(object sender, RoutedEventArgs e) {
                  --titleCount;
                  Title = titleCount.ToString();
              }
              public MainWindow() { InitializeComponent(); DataContext = this; }
              public string MainWindowTitle {
                  get { return mainWindowTitleBase; }
                  set {
                      value = value.Trim();
                      if (value != mainWindowTitleBase) {
                          mainWindowTitleBase = value;
                          OnPropertyChanged("MainWindowTitle");
                      }
                  }
              }
              string mainWindowTitleBase = "Default Title";
              public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
              int titleCount = 0;
              protected void OnPropertyChanged(string /*property name*/ ptyNam) {
                  PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(ptyNam));
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-05
        • 2015-06-06
        • 2016-10-20
        • 2018-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多