下面的代码,程序运行后,是有效的(第一次)。不过,之后就无效了。例如,如果你修改任务栏的位置,那么,WorkArea变化了,但是,程序界面的Width和Height却没有跟着变化。怎么解决?
<Window 其它内容省略 Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}" Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}">
</Window>
<Window 其它内容省略 Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}" Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}">
</Window>
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height,Mode=TwoWay}"
第1次也不行吧
Window 怎么可能等于WorkArea呢
就算无边框的也不行
Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
Left="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Left}"
Top="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Top}"
懂吧,
就是只绑定一次
你必须使用能改变界面的接口如INotifyProperty,附加属性等,或动态资源
界面才能改变
http://msdn.microsoft.com/zh-cn/library/ms750613.aspx
注意这段话:
静态资源引用不会基于运行时行为(例如重新加载页)进行重新求值
我下面的方法,成功绑定了系统的颜色和程序背景色:我在系统的控制面板,修改了系统的背景颜色,然后程序的界面背景颜色,也动态地跟着变化了。
不知道下面这个方法,如何能套用到我的需求上?<Window …省略…Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}" ></Window>
解决方法是
DataContext="{DynamicResource {x:Static SystemParameters.WorkAreaKey}}"
然后直接绑定数据,如
Height="{Binding Path=Height}"
再实现INotifyPropertyChanged接口
这样比较方便,也便于管理
数据类:
public class DemoCustomer : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string customerName; public string CustomerName
{
get { return customerName; }
set
{
customerName = value;
NotifyPropertyChanged(CustomerName); //注意是大写写的属性
}
}
}再在xaml后台设定数据源:
DemoCustomer customer=new DemoCustomer(); //最好调成全局变量,整个类可用,可改变属性
customer.CustomerName="xxxx";
this.DataContext=customer;界面绑定:
Height="{Binding CustomerName}"
这样敲的好累
Height="{Binding Path=Height}"
Title="{Binding Path=Height,Mode=TwoWay}"
></Window>
DataContext="{DynamicResource {x:Static SystemParameters.WorkAreaKey}}"
Height="{Binding Path=Height,Mode=TwoWay}"
Width="{Binding Path=Width,Mode=TwoWay}"
Left="{Binding Path=X,Mode=TwoWay}"
Top="{Binding Path=Y,Mode=TwoWay}"
></Window>