我有一個問題ListBoxItem和DataTrigger。當用滑鼠單擊芯片專案時,我想更改我的專案的Background顏色。Chip
我認為問題出在這行代碼中
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem},Path=IsSelected}"
Value="True">
<Setter Property="IconBackground"
Value="#1b5eb2" />
<Setter Property="Opacity"
Value="1"></Setter>
</DataTrigger>
運行我的代碼,我得到了一個信號類別串列(如下面的螢屏截圖所示):

但是,當我用滑鼠單擊它們時,圓圈的顏色不會改變。
它應該像這樣作業(單擊時):

我嘗試了幾件事并檢查了很多關于 的主題DataTrigger,但我似乎無法弄清楚。
我的 .xaml 視圖
<ListBox Name="SignalCategories"
ItemsSource="{Binding DefinedSignalCategories}"
SelectionMode="Single"
HorizontalAlignment="Center"
VerticalAlignment="Top"
BorderThickness="0">
<!--changing default ListBoxItem to hide selection highlight-->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<!--changing default orientation-->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<!--custom view-->
<materialDesign:Chip Content="{Binding Name}"
Margin="5"
IconForeground="{DynamicResource PrimaryHueDarkForegroundBrush}">
<materialDesign:Chip.Style>
<Style TargetType="{x:Type materialDesign:Chip}">
<Setter Property="Opacity"
Value="0.85" />
<Setter Property="IconBackground"
Value="Gray"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Opacity"
Value="1" />
<Setter Property="IconBackground"
Value="#1b5eb2">
</Setter>
</Trigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem},Path=IsSelected}"
Value="True">
<Setter Property="IconBackground"
Value="#1b5eb2" />
<Setter Property="Opacity"
Value="1"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</materialDesign:Chip.Style>
<materialDesign:Chip.Icon>
<materialDesign:PackIcon Kind="{Binding IconName}" />
</materialDesign:Chip.Icon>
</materialDesign:Chip>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
uj5u.com熱心網友回復:
這不起作用的原因是您濫用了Chip控制元件。它是一種
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/435599.html
標籤:wpf xml 控制模板 数据触发器 xaml 中的材料设计
上一篇:獲取按下按鈕的網格行和網格列
下一篇:按鈕圓角和禁用觸發器不起作用
