11

をに定義したいと思いButtonます。だから私はリソースで、定義された 、 、、ボタンを作成しました...そしてすべてが大丈夫でした。ContentPathResourcesUserControlPathBrushesStyle

<UserControl ...>
    <UserControl.Resources>
        <Path x:Key="PageSmallIcon2" Stretch="Fill" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}, Path=Foreground}" Stroke="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}, Path=Foreground}" SnapsToDevicePixels="True" Data="F1 M 19,25L 27,25L 27,33L 19,33L 19,25 Z M 19,36L 27,36L 27,44L 19,44L 19,36 Z M 31,25L 57,25L 57,33L 31,33L 31,25 Z M 19,47L 27,47L 27,55L 19,55L 19,47 Z "/>
        <SolidColorBrush x:Key="MainColorBrush2" Color="Orange"/>
        <SolidColorBrush x:Key="WhiteColorBrush2" Color="WhiteSmoke"/>
        <Style x:Key="TransparentButtonStyle2" TargetType="{x:Type Button}">
            <Setter Property="Foreground" Value="{StaticResource WhiteColorBrush2}"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="Width" Value="25"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Fill="Transparent"/>
                            <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" Margin="2" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Foreground" Value="{StaticResource MainColorBrush2}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>
        ...
    </Grid>
</UserControl>

これらのリソースを別の に配置することにしましたResourceDictionariesPaths私はもっ​​と多くのことを計画していますBrushes... また、ボタンのスタイルを 1 つだけにしたいと考えています。基本的に私はこれを行っています。これは、ボタンごとにスタイルがありPath、それぞれの特定のスタイルがのButton一部だったからです。ButtonControlTemplate

だから私は3つ作成ResourceDictionaries Colors.xamlIcons.xamlControls.xamlから更新しましたApp.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Theme/Colors.xaml"/>
            <ResourceDictionary Source="Theme/Icons.xaml"/>
            <ResourceDictionary Source="Theme/Controls.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

それを押しても現在PathFillプロパティは更新されませんButton。なぜそれが別々に機能しないのかを説明しResourceDictionaries、この問題の解決策を教えてください。

アップデート

だから私は問題がにあることを発見しましたPath。私がそれを持っているPathと、すべて<UserControl.Resources>の色Colors.xamlとボタンスタイルがControls.xaml再び機能します。

私はまた、私が持っていて、それをボタンのコンテンツとして使用していることを発見PathしましIcons.xamlUserControl

<Button Command="{Binding Path=SmallPageSizeCommand}" Style="{StaticResource TransparentButtonStyle2}" Content="{StaticResource PageSmallIcon2}"/>

and I have multiple instances of my UserControl... thenPathは、一度に a の 1 つのインスタンスにのみ存在しますUserControl

4

1 に答える 1