你好 !我有一个WPF应用程序,我想设置窗口页面的标题而不刷新所有页面,因为在此页面中,我有两个按钮,列出了datarow时属于标题。
void refreshStatusBar()
{
this.Title= "Holaaa";
}
WPF类:
<Height=.... Title="Prueba"...> the initial value
问题是当我按下一个按钮(下一个或背面)时,我需要设置页面标题,而当我在btnext或btback方法中调用refreshstatusbar()时,切勿更改。
我试图绑定标题,但不起作用。始终显示相同的值,初始值:
Title="{Binding Path="windowTitleBar"}"
public String windowTitleBar {get; set;}
void refreshStatusBar(){
windowTitleBar="Holaaa";
}
我要在按下一些按钮时更改标题。我在窗口页面内没有页面,只需显示一件事或另一件事即可。
我也尝试了:
Title="{Binding Path=windowTitleBar, RelativeSource={RelativeSource Mode=Self}}"
和不工作。
请有任何解决方案吗?
对不起我的英语!
谢谢!
这对我有用而没有绑定:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.Title = "Hellooo";
}
void RefreshStatusBar()
{
this.Title = "Holaaa";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
RefreshStatusBar();
}
}
如果要使用绑定,请像Title="{Binding Path=WindowTitleBar, RelativeSource={RelativeSource Mode=Self}}"
但是,wpf无法知道您的属性价值何时更改。您可以实现INotifyPropertyChanged
来解决此问题:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _windowTitleBar = "Hellooo";
public MainWindow()
{
this.WindowTitleBar = "Hellooo";
InitializeComponent();
}
public string WindowTitleBar
{
get { return _windowTitleBar; }
set
{
_windowTitleBar = value;
OnPropertyChanged("WindowTitleBar");
}
}
void RefreshStatusBar()
{
this.WindowTitleBar = "Holaaa";
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void button1_Click(object sender, RoutedEventArgs e)
{
RefreshStatusBar();
}
}
编辑:
我只是注意到您说的是"页面"。我从未使用过页面,但是看起来像设置包含您页面的窗口的标题,必须设置WindowTitle
属性。不幸的是,这不是依赖性property,因此您不能使用绑定。但是,您可以直接设置:
void RefreshStatusBar()
{
this.WindowTitle = "Holaaa";
}