下面的代码,程序运行后,是有效的(第一次)。不过,之后就无效了。例如,如果你修改任务栏的位置,那么,WorkArea变化了,但是,程序界面的Width和Height却没有跟着变化。怎么解决?
<Window 其它内容省略  Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}" Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}">
</Window>

解决方案 »

  1.   

    你没有设置绑定的方向,默认是OneWay的 
    Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height,Mode=TwoWay}" 
      

  2.   

    你先绑定到 Textblock 上,看看值有没有变化。
      

  3.   

    你的代码有问题吧,
    第1次也不行吧
    Window 怎么可能等于WorkArea呢
    就算无边框的也不行
      

  4.   

    第一次行啊,我的就是无边框。第一次就有效果,窗体的大小(长和宽),就刚刚好等于WorkArea的大小。之后,就不会跟随WorkArea的变化而变化了。
      

  5.   

    你试试下面这个,第一次的效果更明显:         Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
            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}"
      

  6.   

    还有你绑定的是静态资源
    懂吧,
    就是只绑定一次
    你必须使用能改变界面的接口如INotifyProperty,附加属性等,或动态资源
    界面才能改变
      

  7.   

    不太明白你的意思。我要实现的效果是,窗体跟随WorkArea的大小变化而变化。例如:拖高、拖低任务栏,窗体的高度依然能够跟随。
      

  8.   

    你的意思就是说,WorkArea变化了,但是这个信息,并没有传达到我的程序?
      

  9.   

    是的看看这个:
    http://msdn.microsoft.com/zh-cn/library/ms750613.aspx
    注意这段话:
     静态资源引用不会基于运行时行为(例如重新加载页)进行重新求值
      

  10.   

    谢谢你。那么,有什么办法帮定一个动态的?
    我下面的方法,成功绑定了系统的颜色和程序背景色:我在系统的控制面板,修改了系统的背景颜色,然后程序的界面背景颜色,也动态地跟着变化了。
    不知道下面这个方法,如何能套用到我的需求上?<Window  …省略…Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}" ></Window>
      

  11.   

    因为是static的类,没有通知机制。直接绑定的话,WPF无法接收到通知。
    解决方法是
    DataContext="{DynamicResource {x:Static SystemParameters.WorkAreaKey}}"
    然后直接绑定数据,如
    Height="{Binding Path=Height}"
      

  12.   

    你可以将所有需要在运行时改变的值放在一个类里,
    再实现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}"
      

  13.   

    你最好行看下WPF的绑定机制和运行机制,
    这样敲的好累
      

  14.   

    直接绑定的话,WPF无法接收到通知。
      

  15.   

    成功了:<Window  省略内容 DataContext="{DynamicResource {x:Static SystemParameters.WorkAreaKey}}" 
    Height="{Binding Path=Height}"
    Title="{Binding Path=Height,Mode=TwoWay}"
    ></Window>
      

  16.   

    成功:<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>