gpt4 book ai didi

c# - System.Windows.SystemParameters.PrimaryScreenWidth 与转换器的 WPF 绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 14:15:31 34 4
gpt4 key购买 nike

我正在尝试将 System.Windows.SystemParameters.PrimaryScreenWidth 绑定(bind)到 ColumnDefinition 的(来自“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}}"/>

这是我的转换器代码隐藏(省略了“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);
}
}

现在,我已经能够在使用“按钮”对象宽度并通过转换器运行它的稍微不同的场景中成功绑定(bind),所以我认为问题出在我尝试绑定(bind)的方式上“ElementName=System.Windows.SystemParameters”。感谢任何帮助,谢谢。

最佳答案

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

Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth},
Converter=...}"

关于c# - System.Windows.SystemParameters.PrimaryScreenWidth 与转换器的 WPF 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9961212/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com