我试图将 System.Windows.SystemParameters.PrimaryScreenWidth 绑定到 ColumnDefinition 的(来自“Grid”内)宽度属性,并使用转换器将“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);
}
}
现在,我已经能够在使用“按钮”对象宽度并通过转换器运行它的稍微不同的场景中成功绑定,所以我认为问题在于我如何尝试从“ElementName”进行绑定=系统.Windows.系统参数”。任何帮助表示赞赏,谢谢。
ElementName
用于 XAML 中的其他元素;为此你需要类似的东西x:Static http://msdn.microsoft.com/en-us/library/ms742135.aspx, e.g.
Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth},
Converter=...}"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)