gpt4 book ai didi

c# - 如何设置 ContentPresenter 内容背景

转载 作者:行者123 更新时间:2023-11-30 20:55:01 24 4
gpt4 key购买 nike

我有一个 ListBox,它绑定(bind)到动态创建的 UserControlsObesvableCollection

<ListBox x:Name="myListBox">
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemsSource" Value="{Binding userControlsCollection}"/>
....
</Style>
</LIstBox.Style>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="Selector.Selected" Handler="ListBox_Selected"/>
<EventSetter Event="Selector.Unselected" Handler="ListBox_UnSelected"/>
<Setter Property="Background" Value="{DynamicResource DefaultBackground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">

<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="true"
Width="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor,AncestorType=ListBoxItem,AncestorLevel=1}}"
Height="{Binding ActualHeight,RelativeSource={RelativeSource FindAncestor,AncestorType=ListBoxItem, AncestorLevel=1}}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox>

我想设置所选控件的背景应该是这样的

<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource SelectedBackground}"/>
</Trigger>
</Style.Triggers>

但是这样我设置了 ListBoxItem Background 并且它不会传播 UserControls Background...

我现在解决它的方法是像这样使用 Selector.Selected/UnSelected 事件处理程序

private void ListBox_Selected(object sender, RoutedEventArgs e)
{

var item = e.Source as ListBoxItem;
var ctrl= item.Content as myControl;

if (ctrl!= null)
{
ctrl.Background = new SolidColorBrush(DefaultSelectedBackground);

}
}

任何想法将不胜感激

最佳答案

尽量保持 ItemContainerStyle 简单。如果您需要弄乱项目的 Template,请使用 ItemTemplateRelativeSource 绑定(bind)来实现您的需要。

为了满足您对 RelativeSource 绑定(bind)的要求,我只需要像这样的 ItemContainerStyle:

<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background"
Value="BurlyWood" />
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter Property="VerticalContentAlignment"
Value="Stretch" />
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="Tomato" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="CadetBlue" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>

现在要在 UserControl 中获取此 Background,我的 UserControl xaml 将如下所示:

<UserControl ...
Background="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBoxItem}},
Path=Background}">

就是这样,我们已经排序了。

您可以从以下位置获取此演示:Here

更新:

如果您正在从代码隐藏创建 UserControl(不知道为什么您需要 但无论如何),也要在代码隐藏中分配绑定(bind)。像这样的东西:

public UserControl CreateUserControl(string text) {
Binding binding = new Binding {
Path = new PropertyPath(BackgroundProperty),
RelativeSource = new RelativeSource() {
Mode = RelativeSourceMode.FindAncestor,
AncestorType = typeof(ListBoxItem)
}
};
var uc = new UserControl {
Content = new TextBlock {
Text = text,
FontSize = 24,
FontWeight = FontWeights.Bold,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
}
};

BindingOperations.SetBinding(uc, BackgroundProperty, binding);
return uc;
}

ItemContainerStyle 将保持与以前相同,您应该完成了。

此方法的演示:Here

关于c# - 如何设置 ContentPresenter 内容背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18607930/

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