【问题标题】:WPF creating resource dictionary that has many bindable parametersWPF 创建具有许多可绑定参数的资源字典
【发布时间】:2020-06-21 20:33:54
【问题描述】:

我有 5 个 WPF GeometryDrawing 对象,它们被放置在资源字典中,因为它们可以在不同的视图中使用。 GeometryDrawing 的 Brush 属性应绑定到具体视图模型中的 Enum 属性。 5 个 GeometryDrawing 对象中的任何一个都可以放置到 WPF Image 中(见下文),它取决于另一个 Enum 属性。如何实现?请让我知道是否需要任何其他说明。谢谢。

<Image Source="Should be any of [image1, image2, image3, image4, image4]"></Image>

资源字典:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Geometry x:Key="geom1">#Some geometry</Geometry>
      <DrawingGroup x:Key="group1">
        <GeometryDrawing Brush="{Binding Path=Background,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom1}" />
      </DrawingGroup>
      <DrawingImage x:Key="image1" Drawing="{StaticResource group1}" />

      <Geometry x:Key="geom2">#Some geometry</Geometry>
      <DrawingGroup x:Key="group2">
        <GeometryDrawing Brush="{Binding Path=Background,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom2}" />
      </DrawingGroup>
      <DrawingImage x:Key="image2" Drawing="{StaticResource group2}" />

      <Geometry x:Key="geom3">#Some geometry</Geometry>
      <DrawingGroup x:Key="group3">
        <GeometryDrawing Brush="{Binding Path=Background,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom3}" />
      </DrawingGroup>
      <DrawingImage x:Key="image3" Drawing="{StaticResource group3}" />

      <Geometry x:Key="geom4">#Some geometry</Geometry>
      <DrawingGroup x:Key="group4">
        <GeometryDrawing Brush="{Binding Path=Background,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Geometry="{StaticResource geom4}" />
      </DrawingGroup>
      <DrawingImage x:Key="image4" Drawing="{StaticResource group4}" />

      <Geometry x:Key="geom5">#Some geometry</Geometry>
      <DrawingGroup x:Key="group5" >
        <GeometryDrawing Brush="{Binding Path=Background,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}"{StaticResource geom5}" />
      </DrawingGroup>
      <DrawingImage x:Key="image5" Drawing="{StaticResource group5}" />

    <DataTemplate x:Key="Icon1">
            <Image  Source="{StaticResource image1}"/>
        </DataTemplate>

<DataTemplate x:Key="Icon2">
            <Image  Source="{StaticResource image2}"/>
        </DataTemplate>

<DataTemplate x:Key="Icon3">
            <Image  Source="{StaticResource image3}"/>
        </DataTemplate>

<DataTemplate x:Key="Icon4">
            <Image  Source="{StaticResource image4}"/>
        </DataTemplate>

<DataTemplate x:Key="Icon5">
            <Image  Source="{StaticResource image5}"/>
        </DataTemplate>
    </ResourceDictionary>

现在我在 View 中使用它(为了简单起见,ContentControl 中的 Background="Green",以后可以很容易地绑定它。):

 <ContentControl Background="Green">
        <ContentControl.Style>
            <Style TargetType="ContentControl">
                <Setter Property="ContentTemplate" Value="{StaticResource Icon1}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=MainWindow1, Path=Id}" Value="0">
                        <Setter Property="ContentTemplate" Value="{StaticResource Icon2}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=MainWindow1, Path=Id}" Value="1">
                        <Setter Property="ContentTemplate" Value="{StaticResource Icon3}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>

在设计时图像完美显示,但在运行时图像未呈现

链接到回购Sample project

【问题讨论】:

  • 我认为您将需要使用数据模板来执行此操作。 Drawinggroup 是可冻结的,因此如果您将其放入资源字典中,它会在您取出时被冻结。您需要克隆或解冻它以更改任何属性。你可以证明这很容易。给那些刷子。合并您的资源字典。使用 application.current.resources["group5"] 或其他方法将其中一个组取出。看看它是否被冻结或只是尝试设置画笔看看会发生什么。
  • @Andy 感谢您的回复。我几乎解决了我的问题,但是现在图像没有在运行时呈现,而在设计时预览是可以的。请查看更新的代码。

标签: c# wpf resources


【解决方案1】:

我找到了解决方案。这篇文章帮助了我Bind to GeometryDrawing brush。感谢克莱门斯。但是为此我不得不去掉GeometryDrawing,因为我没有找到绑定Brush属性的方法。

现在我的资源字典只包含几何对象:

<ResourceDictionary x:Name="IconsResource" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApp6">

    <Geometry x:Key="ErrorOutline">F1 M24,24z M0,0z M11,15L13,15 13,17 11,17z M11,7L13,7 13,13 11,13z M11.99,2C6.47,2 2,6.48 2,12 2,17.52 6.47,22 11.99,22 17.52,22 22,17.52 22,12 22,6.48 17.52,2 11.99,2z M12,20C7.58,20 4,16.42 4,12 4,7.58 7.58,4 12,4 16.42,4 20,7.58 20,12 20,16.42 16.42,20 12,20z</Geometry>
    <Geometry x:Key="Check_circle">F1 M24,24z M0,0z M12,2C6.48,2 2,6.48 2,12 2,17.52 6.48,22 12,22 17.52,22 22,17.52 22,12 22,6.48 17.52,2 12,2z M10,17L5,12 6.41,10.59 10,14.17 17.59,6.58 19,8 10,17z</Geometry>
    <Geometry x:Key="New_releases">F1 M24,24z M0,0z M23,12L20.56,9.22 20.9,5.54 17.29,4.72 15.4,1.54 12,3 8.6,1.54 6.71,4.72 3.1,5.53 3.44,9.21 1,12 3.44,14.78 3.1,18.47 6.71,19.29 8.6,22.47 12,21 15.4,22.46 17.29,19.28 20.9,18.46 20.56,14.78 23,12z M13,17L11,17 11,15 13,15 13,17z M13,13L11,13 11,7 13,7 13,13z</Geometry>
    <Geometry x:Key="Sync_problem">F1 M24,24z M0,0z M3,12C3,14.21,3.91,16.2,5.36,17.64L3,20 9,20 9,14 6.76,16.24C5.68,15.15 5,13.66 5,12 5,9.39 6.67,7.17 9,6.35L9,4.26C5.55,5.15,3,8.27,3,12z M11,17L13,17 13,15 11,15 11,17z M21,4L15,4 15,10 17.24,7.76C18.32,8.85 19,10.34 19,12 19,14.61 17.33,16.83 15,17.65L15,19.74C18.45,18.85 21,15.73 21,12 21,9.79 20.09,7.8 18.64,6.36L21,4z M11,13L13,13 13,7 11,7 11,13z</Geometry>
    <Geometry x:Key="Error">F1 M24,24z M0,0z M12,2C6.48,2 2,6.48 2,12 2,17.52 6.48,22 12,22 17.52,22 22,17.52 22,12 22,6.48 17.52,2 12,2z M13,17L11,17 11,15 13,15 13,17z M13,13L11,13 11,7 13,7 13,13z</Geometry>
</ResourceDictionary>

主要思想是使用Path元素,所以:

<Window x:Class="WpfApp6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Path Stroke="{Binding ElementName=ColorBox, Path=Text}" Fill="{Binding ElementName=ColorBox, Path=Text}">
                    <Path.Style>
                        <Style TargetType="Path">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=TextBox, Path=Text}" Value="1">
                                    <Setter Property="Data" Value="{StaticResource Check_circle}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=TextBox, Path=Text}" Value="2">
                                    <Setter Property="Data" Value="{StaticResource Error}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=TextBox, Path=Text}" Value="3">
                                    <Setter Property="Data" Value="{StaticResource ErrorOutline}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=TextBox, Path=Text}" Value="4">
                                    <Setter Property="Data" Value="{StaticResource New_releases}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=TextBox, Path=Text}" Value="5">
                                    <Setter Property="Data" Value="{StaticResource Sync_problem}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Path.Style>
                </Path>
            <TextBox Name="TextBox"></TextBox>
            <TextBox Name="ColorBox">Green</TextBox>
        </StackPanel>
    </Grid>
</Window>

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多