0
我有一個元素列表(純文本塊的簡單按鈕),它們是根據列表項內容進行顏色編碼的。用戶可以更新Listitem,因此listitem的顏色應該改變。對於某些listitem背景顏色,比如「Red」,我想添加一個模式。將背景顏色更改爲基於可視模式
我在XAML中添加以下VisualPatterns:在列表項使用
<Window.Resources>
<VisualBrush x:Key="FwdPattern" TileMode="Tile" Viewport="0,0,15,15" ViewportUnits="Absolute" Viewbox="0,0,15,15" ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Grid>
<Path Data="M 0 15 L 15 0" Stroke="Gray" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
<VisualBrush x:Key="BckPattern" TileMode="Tile" Viewport="0,0,15,15" ViewportUnits="Absolute" Viewbox="0,0,15,15" ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Grid>
<Path Data="M 0 0 L 15 15" Stroke="Gray" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Window.Resources>
按鈕模板是:
<Border Background="{Binding BackgroundClr}">
<Button Name="MyButton" Content="Testing">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{Binding BackgroundClr}"/>
<Style.Triggers>
<!-- This does not work, see [http://stackoverflow.com/questions/39583263/brush-mvvm-binding-does-not-give-named-color/39583422#39583422][1] -->
<DataTrigger Binding="{Binding BackgroundClr}" Value="Red">
<Setter Property="Background" Value="{StaticResource BckPattern}"/>
</DataTrigger>
<!-- This does not work either, it goes in infinite loop
and StackOverflow exception is thrown-
probably because I am reading the background color in
the datatrigger and again updating it- but i dont know-->
<DataTrigger Binding="{Binding Background.Color, RelativeSource={RelativeSource Self}}" Value="Red">
<Setter Property="Background" Value="{StaticResource BckPattern}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Border>
目前我除了從VM按鈕BackgroundClr沒有其他的知識,以確定是否我需要提供一個模式或不。
試圖解
- 一種解決方案是有一個結合性財產PatternName並基於它,確定要應用的圖案:
上面的代碼的工作,但我必須有一個附加的場所在VM
- 另一個解決方案是將訪問VisualBrush在VM和BackgroundClr直接應用圖案 - 我還沒有想出如何做到這一點呢。
哪個更好的解決方案還是有其他方法可以實現?
感謝,
RDV