内部数据模板时WPF动态资源不更新

时间:2012-05-30 05:36:14

标签: wpf xaml resourcedictionary dynamicresource drawingbrush

我正在尝试在WPF应用中设置系统,用户可以在其中选择不同的主题和UI更新。为实现这一点,我将删除一个资源字典并合并到另一个资源字典中。

除了一种情况外,一切正常。

我有一些定义datatemplate的页面。在datatemplate中是一个矩形,它绑定到viewmodel上的字符串属性(使用Caliburn.Micro),我有一个转换器将字符串转换为绘图笔(它从资源中找到它)。

问题是画笔中的动态资源(颜色)不会更新。

矩形的每个其他实例我都以相同的方式绑定(不在数据模板中)绘图画笔的颜色完美更新。只有在datatemplate内部才会出现此问题。

以下是视图

上的数据模板示例
<DataTemplate x:Key="ListBoxItemTemplate">
        <Border Style="{DynamicResource EntitySelectListBox}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Rectangle Grid.Column="0" Fill="{Binding IconName, Converter={converters:StringToResourceConverter}}" Width="50" Height="50" Margin="5" ToolTip="{Binding ToolTip}" />

                <TextBlock FontFamily="Segoe WP"
                           FontWeight="Light"
                           FontSize="22"
                           Grid.Column="1"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center"
                           Text="{Binding ButtonText}" />
            </Grid>
        </Border>
    </DataTemplate>

以下是具有颜色动态资源的绘图画笔的示例:

<DrawingBrush x:Key="BankIcon" Viewbox="0,0,442,442" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Transform>
                <MatrixTransform Matrix="9.20833333333333,0,0,9.20833333333333,0,0"/>
            </DrawingGroup.Transform>
            <DrawingGroup>
                <DrawingGroup.Transform>
                    <MatrixTransform Matrix="1,0,0,1,0,2.04885336170188"/>
                </DrawingGroup.Transform>
                <GeometryDrawing Brush="{DynamicResource SecondaryText}" Geometry="M32.015748,33.342608 L46.283901,33.342608 C47.231779,33.342608 48,34.111116 48,35.061416 L48,40.968297 29.109026,40.968297 C31.057581,39.645195 32.207827,37.567466 32.207827,35.105579 32.207827,34.463728 32.135616,33.879828 32.015748,33.342608 z M1.7188548,33.342608 L21.193252,33.342608 C21.734045,33.580305 22.220392,33.759667 22.584643,33.892346 24.90359,34.727827 24.90359,35.253493 24.90359,35.449457 24.90359,35.986481 23.92294,36.182345 23.261798,36.182345 21.415607,36.182345 19.949486,35.468384 19.243114,35.041909 L17.321123,33.889921 15.507445,40.263165 16.585441,40.951892 0,40.968297 0,35.061416 C8.7927075E-13,34.111116 0.76985754,33.342608 1.7188548,33.342608 z M22.790523,18.230994 L25.366021,18.230994 25.366021,21.121156 C27.345029,21.183175 28.700861,21.687368 29.703128,22.191564 L28.856339,25.079107 C28.100782,24.702539 26.716707,23.982205 24.582318,23.982205 22.381153,23.982205 21.594926,25.110746 21.594926,26.179891 21.594926,27.467599 22.72608,28.221998 25.431822,29.259503 28.949705,30.578852 30.489161,32.27603 30.489161,35.105438 30.489161,37.771993 28.636319,40.099727 25.209281,40.667104 L25.209281,43.902294 22.602338,43.902294 22.602338,40.855485 C20.623328,40.792207 18.644319,40.22619 17.510837,39.502072 L18.362478,36.519611 C19.618635,37.270223 21.344919,37.898937 23.261813,37.898937 25.271298,37.898937 26.620819,36.927818 26.620819,35.449302 26.620819,34.003588 25.522762,33.125251 23.167186,32.27603 19.901545,31.081883 17.733186,29.541929 17.733186,26.651183 17.733186,23.9494 19.618635,21.844013 22.790523,21.279064 z M40.239403,16.937238 L44.995147,16.937238 44.995147,31.067614 40.239403,31.067614 z M32.794241,16.937238 L37.545128,16.937238 37.545128,31.067614 32.794241,31.067614 z M10.453898,16.937238 L15.205758,16.937238 15.205758,31.067614 10.453898,31.067614 z M3.0077641,16.937238 L7.7625359,16.937238 7.7625359,31.067614 3.0077641,31.067614 z M23.952052,0 L24.047946,0.0024883747 C24.709093,0.017653378 25.175256,0.11243361 26.113014,0.68999864 L48,14.939825 24.047946,14.939825 23.952052,14.939825 0,14.939825 21.885821,0.68999864 C22.824745,0.11243361 23.292458,0.017653378 23.952052,0.0024883747 z"/>
            </DrawingGroup>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

这是一个导入其他资源字典的资源字典。一个用于样式,一个用于矢量图(它们在皮肤之间共享)。

ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Class="SixtenLabs.CashFlow.Client.Skins.Monochrome">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Resources/CommonStyles.xaml"/>
    <ResourceDictionary Source="/Resources/VectorIcons.xaml"/>

我尝试将各个部分移动到不同的地方(app.xaml,window.resources)无济于事。任何想法都将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

尝试关闭ListBox中的虚拟化,因为您可能正在回收容器,并且容器不会总是遵循DynamicResource绑定中的更改。

<ListBox VirtualizingStackPanel.IsVirtualizing="False"
         VirtualizingStackPanel.VirtualizationMode="Standard" />