gpt4 book ai didi

wpf - 基于祖先类型的存在设置样式

转载 作者:行者123 更新时间:2023-12-04 14:59:26 26 4
gpt4 key购买 nike

我有 2 套 TextBlocks其中一些在 ItemControl其中一些不是。
我想制作一种样式(仅基于类型)来设置 TextBlock 的背景如果它的祖先是 ItemControl .
我可以使用下面的代码来做到这一点,但我的问题是在日志(和输出窗口)上显示了一条数据投标错误消息,因为 TextBlocks哪些没有 ItemControl作为他们的祖先。
有没有更好的方法来完成此任务并避免出现此错误消息?

<Grid>
<Grid.Resources>
<local:HasAncestorConverter x:Key="HasAncestorConverter" />
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ItemsControl}},
Converter={StaticResource HasAncestorConverter}}" Value="True">
<Setter Property="Background"
Value="{Binding Tag,
RelativeSource={RelativeSource
AncestorType={x:Type ItemsControl}}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<StackPanel>
<TextBlock Text="Out of ItemControl" />
<ItemsControl Tag="Blue" >
<TextBlock Text="Inside of ItemControl" />
</ItemsControl>
</StackPanel>
</Grid>
转换器:
class HasAncestorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
错误信息:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'NoTarget' (type 'Object')

最佳答案

我认为@Xameli 解决方案正是您真正要寻找的...
但如果你只是必须以一种风格来做,那么你可以使用 VisualTreeHelper 来实现它像那样:

<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Converter={StaticResource HasAncestorConverter}}" Value="True">
<Setter Property="Background"
Value="{Binding Tag,RelativeSource={RelativeSource Self}}" />

</DataTrigger>
</Style.Triggers>

转换器:
class HasAncestorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//you probably will have to look a few levels up
var parent = VisualTreeHelper.GetParent(value) as ItemsControl;
return item != null;
}

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

关于wpf - 基于祖先类型的存在设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645660/

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