使用转换器从System.Windows.SystemParameters.PrimaryScreenWidth进行WPF绑定 [英] WPF Binding from System.Windows.SystemParameters.PrimaryScreenWidth with a Converter
问题描述
我试图将System.Windows.SystemParameters.PrimaryScreenWidth绑定到ColumnDefinition的(在 Grid中)Width属性,并使用转换器将 PrimaryScreenWidth转换为 GridLength。但是它永远不会进入转换代码。
I'm trying to bind System.Windows.SystemParameters.PrimaryScreenWidth to a ColumnDefinition's (From within a 'Grid') Width property, and using a converter to convert the 'PrimaryScreenWidth' into a 'GridLength'. But it never gets inside the 'convert' code.
这是我的XAML:
<Window.Resources>
<local:ScreenWidthToLeftBarWidth x:Key="leftBarConverter" />
</Window.Resources>
<ColumnDefinition Width="{Binding ElementName=System.Windows.SystemParameters, Path=PrimaryScreenWidth, Converter={StaticResource leftBarConverter}}"/>
这是转换器的我的CodeBehind(省略了 ConvertBack方法:
Here's my CodeBehind for the converter (leaving out the 'ConvertBack' method:
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);
}
}
现在,在使用按钮对象Width和通过转换器运行它,所以我认为问题出在我如何尝试从 ElementName = System.Windows.SystemParameters进行绑定。感谢您的帮助。
Now, I've been able to bind successfully in a slightly different scenario of using a 'Button' objects Width and running it through a converter, So I'm thinking the problem is with how I'm trying to bind from the "ElementName=System.Windows.SystemParameters". Any help appreciated, thanks.
推荐答案
ElementName
用于XAML中的其他元素;为此,您将需要 x:Static
,例如
ElementName
is for other elements in XAML; for this you will need something like x:Static
, e.g.
Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth},
Converter=...}"
这篇关于使用转换器从System.Windows.SystemParameters.PrimaryScreenWidth进行WPF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!