使用转换器从System.Windows.SystemParameters.PrimaryScreenWidth绑定WPF

Run*_*tar 3 c# data-binding wpf xaml

我试图System.Windows.SystemParameters.PrimaryScreenWidth绑定到ColumnDefinition的(从"网格"内)Width属性,并使用转换器将"PrimaryScreenWidth"转换成"GridLength".但它永远不会进入'转换'代码.

这是我的XAML:

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

这是转换器的CodeBehind(省略'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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在,我已经能够在稍微不同的情况下成功绑定使用'Button'对象Width并通过转换器运行它所以我认为问题在于我是如何尝试从"ElementName"绑定的= System.Windows.SystemParameters".任何帮助表示感谢,谢谢.

H.B*_*.B. 7

ElementName适用于XAML中的其他元素; 为此你需要一些像x:Static,比如说

Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth},
                Converter=...}"
Run Code Online (Sandbox Code Playgroud)