我有這樣的風格:設定值
<Style x:Key="SelectableListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="4"
BorderThickness="2"
x:Name="IconBorder"
Margin="4,2,4,2">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="IconBorder"
Property="BorderBrush"
Value="Blue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的問題是,我不知道在列表框設置其屬性,消耗我的風格的時候,這樣它的邊界是ListBoxItems會最終得到所需的邊框刷。我也想在我的風格中爲其他邊框筆畫做這項工作。
我希望能夠擁有兩個具有相同風格但不同邊框顏色的列表框。我有這樣的一個列表框中:
<ListBox
ItemsSource="{Binding SelectedProduct.Pictures}"
SelectedItem="{Binding SelectedSet, Mode=TwoWay}"
ItemContainerStyle="{StaticResource ResourceKey= SelectableListBoxItemStyle}">
</ListBox>
Update..I嘗試這樣做:
<ListBox
ItemsSource="{Binding SelectedProduct.Pictures}"
SelectedItem="{Binding SelectedSet, Mode=TwoWay}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource SelectableListBoxItemStyle}">
<Setter TargetName="IconBorder" Property="BorderBrush" Value="Green" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
但是,我得到: 錯誤8 targetName屬性不能在風格二傳手設定。
感謝您的回答。請參閱我已經將列表框的項目容器樣式設置爲粘貼在我的問題中的「SelectableListBoxItemStyle」。問題出在'BorderBrush =「{TemplateBinding BorderBrush}」。當我在xaml中使用Style時,如何設置* this? IE瀏覽器。我應該在列表框上設置哪些屬性,以便我的ListBoxItem的邊框最終具有設置的邊框畫筆? –
再次感謝。第二個工作,但現在我的列表框需要有相同的BorderBrush,因爲它是ListBoxItems,這不是很好。我編輯我的問題添加更新,也許你可以修復:) –
哦,一個不錯的!資源竅門釘住了它。非常感謝你 :) –