2012-04-01 114 views
0

我想System.Windows.SystemParameters.PrimaryScreenWidth Width属性(一个“网格”内,从)绑定到一个ColumnDefinition的,并使用转换器转换'PrimaryScreenWidth'转换为'GridLength'。但它永远不会进入'转换'代码。WPF从System.Windows.SystemParameters.PrimaryScreenWidth绑定一个转换器

这里是我的XAML:

<Window.Resources> 
    <local:ScreenWidthToLeftBarWidth x:Key="leftBarConverter" /> 
</Window.Resources> 
<ColumnDefinition Width="{Binding ElementName=System.Windows.SystemParameters, Path=PrimaryScreenWidth, Converter={StaticResource leftBarConverter}}"/> 

这里是我的转换代码隐藏(而忽略了“ConvertBack”的方法:

public class ScreenWidthToLeftBarWidth : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      double aValue = (double)value; 
      GridLength newWidth = new GridLength(aValue); 
      return (newWidth); 
     } 
    } 

现在,我已经能够在一个稍微成功绑定使用'按钮'对象的不同场景宽度和通过转换器运行它,所以我想这个问题是与我想如何绑定从“ElementName = System.Windows.SystemParameters”。任何帮助表示赞赏,谢谢。

回答

2

ElementName用于XAML中的其他元素;为此,您需要诸如x:Static之类的东西,例如

Width="{Binding Source={x:Statc SystemParameters.PrimaryScreenWidth}, 
       Converter=...}" 
+0

我希望它是动态的(如果用户更改屏幕分辨率,可能会发生更改通知,对不对?)。另外,我需要一个转换器来随着它的动态变化来改变它的值。关于备用解决方案的任何想法? – 2012-04-01 01:45:45

+0

@RuneStar:你可以在与'Source'的绑定中使用它(请参阅编辑),以获取需要订阅某个事件(可能在系统级别,我不知道)的更改通知,然后更新绑定例如通过'BindingExpression.UpdateTarget')。 – 2012-04-01 01:55:20