使用转换器从System.Windows.SystemParameters.PrimaryScreenWidth绑定WP
本文关键字:SystemParameters PrimaryScreenWidth 绑定 WP Windows System 转换器 | 更新日期: 2023-09-27 18:29:42
我正在尝试将System.Windows.SystemParameters.PrimyScreenWidth绑定到ColumnDefinition的(From in a'Grid')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}}"/>
这是我的转换器的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);
}
}
现在,我已经能够在一个稍微不同的场景中成功绑定,即使用"Button"对象Width并通过转换器运行它,所以我认为问题在于我如何尝试从"ElementName=System.Windows.SystemParameters"绑定。如有任何帮助,不胜感激。
ElementName
用于XAML中的其他元素;为此,您将需要类似x:Static
的东西,例如
Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth},
Converter=...}"