WPF создает словарь ресурсов с множеством привязываемых параметров - PullRequest
0 голосов
/ 19 июня 2020

У меня есть 5 объектов WPF GeometryDrawing, которые помещены в словарь ресурсов, потому что их можно использовать в разных представлениях. Атрибут Bru sh объекта GeometryDrawing должен быть привязан к свойству Enum в конкретной модели представления. Любой из 5 объектов GeometryDrawing может быть помещен в изображение WPF (см. Ниже), это зависит от другого свойства 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>

И теперь в представлении я использую его как (для простоты Background = "Green" в ContentControl, это может быть легко будет связан позже.):

 <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>

Во время разработки изображение отображается идеально, но в время выполнения изображение не отображается .

Ссылка на репо Образец проект

1 Ответ

0 голосов
/ 20 июня 2020

Я нашел решение. Этот пост помог мне в этом Bind to GeometryDrawing bru sh. Спасибо Клеменсу. Но для этого мне пришлось избавиться от GeometryDrawing, потому что я не нашел способ привязки атрибута Bru sh.

Теперь мой словарь ресурсов содержит только объекты Geometry:

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...