gpt4 book ai didi

WPF MultiBinding 到后台属性不起作用

转载 作者:行者123 更新时间:2023-12-04 05:12:42 32 4
gpt4 key购买 nike

MultiBinding 到背景属性不起作用。转换后,背景只是变成系统的默认颜色,而不是我在 MultiValueConverter 中设置的颜色。其他一切都已正确设置。我的 MultiBinding 到背景有什么问题?

<Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource triggerResource},
Path=MyIsSelected}"
Value="True">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource groupNameToBackgroundConv}">
<Binding Path="Name" />
<Binding Source="{StaticResource selectedGroupName}" Path="Name" />
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>

我的 MultiValueConverter 是
 public class GroupNameToBackgroundConv : IMultiValueConverter
{
private const string DEFAULT_COLOR = "#B8CBE9";
private const string SELECTED_COLOR = "#FFFF00";

public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string groupName = values[0] as string;
string selectedGroupName = values[1] as string;

if (groupName == null)
return DEFAULT_COLOR;

if (selectedGroupName == null)
return DEFAULT_COLOR;

if (groupName == selectedGroupName)
{
return SELECTED_COLOR;
}
else
{
return DEFAULT_COLOR;
}


} // ends method

public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

} // ends class

最佳答案

我解决了这个问题。
Convert返回值时,需要为Brush,而不是string或Color
控件扩展器的背景属性。
这是我的转换器。

 public class GroupNameToBackgroundConv : IMultiValueConverter
{

private Color DEFAULT_COLOR = (Color)ColorConverter.ConvertFromString("#B8CBE9");
private Color SELECTED_COLOR = (Color)ColorConverter.ConvertFromString("#FFFF00");

public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string groupName = values[0] as string;
string selectedGroupName = values[1] as string;

if (groupName == null)
return ColorOrBrush(DEFAULT_COLOR, targetType);

if (selectedGroupName == null)
return ColorOrBrush(DEFAULT_COLOR, targetType);

if (groupName == selectedGroupName)
{
return ColorOrBrush(SELECTED_COLOR, targetType);
}
else
{
return ColorOrBrush(DEFAULT_COLOR, targetType);
}



} // ends method


private object ColorOrBrush(Color c, Type targetType)
{
if (targetType == typeof(Color))
return c;
else if (targetType == typeof(Brush))
return new SolidColorBrush(c);
else
return null;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

} // ends class

关于WPF MultiBinding 到后台属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14702085/

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