WPF 4.0全球风格问题

本文关键字:风格 问题 WPF | 更新日期: 2023-09-27 17:58:05

我开发了一个WPF应用程序(target.Net 4.0)。我定义了像这样的全球风格

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/BuildingInspector;component/Resources/Languages/fr-FR.xaml" />
            <!--<ResourceDictionary Source="pack://application:,,,/BuildingInspector;component/Resources/ButtonStyles.xaml" />-->
        </ResourceDictionary.MergedDictionaries>
        <ControlTemplate x:Key="ctButton" TargetType="{x:Type telerik:RadButton}">
            <Grid>
                <Border>
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="BorderThickness" Value="0" />
                            <Setter Property="Background" Value="Transparent" />
                        </Style>
                    </Border.Style>
                </Border>
                <ContentPresenter Content="{Binding Path=(app:DependencyProperties.ImagePath), RelativeSource={RelativeSource AncestorType={x:Type telerik:RadButton}}}" />
            </Grid>
        </ControlTemplate>
        <!-- Style par défault des boutons -->
        <Style x:Key="defaultButtonStyle" TargetType="{x:Type telerik:RadButton}">
            <Setter Property="Template" Value="{DynamicResource ctButton}" />
        </Style>
        <!-- Style du bouton "Add" -->
        <Style x:Key="addButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathAdd}" />
            <Setter Property="app:DependencyProperties.Color" Value="LightGreen" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="GreenYellow" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Green" />
        </Style>
        <!-- Style du bouton "Cancel" -->
        <Style x:Key="cancelButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathCancel}" />
            <Setter Property="app:DependencyProperties.Color" Value="Salmon" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="SandyBrown" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Red" />
        </Style>
        <!-- Style du bouton "Delete" -->
        <Style x:Key="deleteButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathDelete}" />
            <Setter Property="app:DependencyProperties.Color" Value="Salmon" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="SandyBrown" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Red" />
        </Style>
        <!-- Style du bouton "Exit" -->
        <Style x:Key="exitButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathExit}" />
            <Setter Property="app:DependencyProperties.Color" Value="Salmon" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="SandyBrown" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Red" />
        </Style>
        <!-- Style du bouton "Next" -->
        <Style x:Key="nextButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathNext}" />
            <Setter Property="app:DependencyProperties.Color" Value="LightGreen" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="GreenYellow" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Green" />
        </Style>
        <!-- Style du bouton "Save" -->
        <Style x:Key="saveButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathSave}" />
            <Setter Property="app:DependencyProperties.Color" Value="LightGreen" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="GreenYellow" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Green" />
        </Style>

        <!-- Style du Path (contenu) des boutons -->
        <Style x:Key="pathButtonStyle" TargetType="Path">
            <Setter Property="Fill"
                    Value="{Binding Path=(app:DependencyProperties.Color), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"
                             Value="True">
                    <Setter Property="Fill" 
                            Value="{Binding Path=(app:DependencyProperties.MouseOverColor), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=IsPressed, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"
                             Value="True">
                    <Setter Property="Fill"
                            Value="{Binding Path=(app:DependencyProperties.PressedColor), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"
                             Value="False">
                    <Setter Property="Fill"
                            Value="{Binding Path=(app:DependencyProperties.DisabledColor), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadButton}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <!-- Contenu du bouton "Add" -->
        <Grid x:Key="pathAdd">
            <Grid Name="addBackgroundGrid" MinWidth="48" MinHeight="48" Visibility="Collapsed" />
            <Path Data="M2.880352,35.504997L25.563028,35.504997 25.461889,35.77055C24.680614,37.90033 24.254,40.199783 24.254,42.596951 24.254,44.994007 24.680614,47.293259 25.461889,49.422794L25.562908,49.688 2.880352,49.688C1.290522,49.688,3.8410201E-07,48.396416,0,46.807915L0,38.38258C3.8410201E-07,36.794079,1.290522,35.504997,2.880352,35.504997z M41.691601,32.867001L41.691601,40.591217 33.968002,40.591217 33.968002,45.48729 41.691601,45.48729 41.691601,53.211002 46.587406,53.211002 46.587406,45.48729 54.311005,45.48729 54.311005,40.591217 46.587406,40.591217 46.587406,32.867001z M44.126499,27.625C52.394669,27.625 59.098,34.325611 59.098,42.596501 59.098,50.859093 52.394669,57.565003 44.126499,57.565003 35.858036,57.565003 29.156,50.859093 29.156,42.596501 29.156,34.325611 35.858036,27.625 44.126499,27.625z M2.880352,17.594002L43.182121,17.594002C44.771912,17.594002,46.061001,18.882946,46.061001,20.471779L46.061001,22.816408 45.653107,22.780068C45.149128,22.741593 44.639954,22.722 44.126297,22.722 37.278214,22.722 31.227619,26.205 27.652683,31.492657L27.472803,31.773003 2.880352,31.773003C1.290522,31.773001,3.8410201E-07,30.484057,8.8062882E-07,28.893322L8.8062882E-07,20.471779C3.8410201E-07,18.882946,1.290522,17.594002,2.880352,17.594002z M8.4794798,3.8200022C6.678607,3.8200022 5.2110004,5.2864023 5.2110014,7.0887662 5.2110004,8.8935586 6.678607,10.357 8.4794798,10.357 10.281624,10.357 11.748,8.8935586 11.748,7.0887662 11.748,5.2864023 10.281624,3.8200022 8.4794798,3.8200022z M2.880352,0L43.182117,0C44.771912,5.590332E-07,46.061001,1.2890898,46.061001,2.8779911L46.061001,11.302441C46.061001,12.893875,44.771912,14.18,43.182117,14.18L2.880352,14.18C1.290522,14.18,3.8410201E-07,12.893875,0,11.302441L0,2.8779911C3.8410201E-07,1.2890898,1.290522,5.590332E-07,2.880352,0z" 
                  Stretch="Uniform" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                  Style="{DynamicResource ResourceKey=pathButtonStyle}">
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
        <!-- Contenu du bouton "Cancel" -->
        <Grid x:Key="pathCancel">
            <Grid Name="cancelBackgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
            <Path Data="M2.9505676,36.439814L21.343939,36.439814C20.303749,39.110218 19.703638,42.000652 19.703638,45.041111 19.703638,47.101423 19.993691,49.091722 20.493784,50.992008L2.9505676,50.992008C1.3202689,50.992008,2.6905189E-05,49.67181,2.6147463E-05,48.041565L2.6147463E-05,39.400261C2.6905189E-05,37.760014,1.3202689,36.439814,2.9505676,36.439814z M43.691771,27.403506C43.883024,27.398506,44.075529,27.398506,44.270537,27.403506L44.280539,27.403506 44.300539,27.403506C46.750612,27.423509,49.070685,27.943573,51.18075,28.873688L51.25075,28.913692C57.48094,31.684034 61.801073,37.954807 61.73107,45.205702 61.631067,54.946903 53.660826,62.767868 43.920527,62.667858 36.320295,62.587849 29.900101,57.707245 27.500027,50.946411L33.810221,49.846275C35.580275,53.706753 39.450392,56.407085 43.980528,56.457092 50.230719,56.517097 55.340876,51.496479 55.400877,45.245708 55.470881,39.004938 50.450728,33.884306 44.200537,33.824298 43.670519,33.824298 43.150503,33.864304 42.64049,33.924311L42.630488,33.884306C40.950438,34.104332,39.390391,34.694407,38.030348,35.584515L40.200415,37.634769C40.33042,37.754784 40.380419,37.944807 40.310415,38.104826 40.250414,38.274847 40.100412,38.384861 39.920405,38.394863L34.650244,38.674896 29.970102,38.924927 29.940102,38.924927C29.810097,38.924927 29.700094,38.86492 29.61009,38.77491 29.520088,38.674896 29.480087,38.54488 29.510088,38.404863L30.260111,34.234348 31.340143,28.333621C31.370144,28.153599 31.500149,28.013581 31.670155,27.983578 31.850159,27.943573 32.030165,28.013581 32.130167,28.1636L33.86022,30.703914C36.260294,28.9737 39.11038,27.853561 42.200476,27.513519 42.240477,27.503518 42.280478,27.503518 42.320479,27.503518 42.590487,27.473515 42.850496,27.453512 43.120504,27.43351 43.310507,27.418509 43.500513,27.408506 43.691771,27.403506z M2.9499468,18.059961L44.308165,18.059961C45.938101,18.059961,47.268,19.380206,47.268,21.020318L47.268,21.830486C45.968126,21.610486 44.638106,21.470574 43.278181,21.470575 34.818579,21.470574 27.428881,25.931133 23.269024,32.611961L2.9499468,32.611961C1.3200119,32.611961,-1.251392E-07,31.291716,0,29.661614L0,21.020318C-1.251392E-07,19.380206,1.3200119,18.059961,2.9499468,18.059961z M8.6996557,3.927825C6.8497344,3.927825 5.3397986,5.4269943 5.3397986,7.2759705 5.3397986,9.1249466 6.8497344,10.634111 8.6996557,10.634111 10.549576,10.634111 12.049512,9.1249466 12.049512,7.2759705 12.049512,5.4269943 10.549576,3.927825 8.6996557,3.927825z M2.949901,0L44.308134,0C45.938063,0,47.268007,1.3292637,47.268007,2.9583616L47.268007,11.603574C47.268007,13.232672,45.938063,14.551941,44.308134,14.551941L2.949901,14.551941C1.3199705,14.551941,2.6895254E-05,13.232672,2.6147463E-05,11.603574L2.6147463E-05,2.9583616C2.6895254E-05,1.3292637,1.3199705,0,2.949901,0z" 
                  Stretch="Uniform" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                  Style="{DynamicResource ResourceKey=pathButtonStyle}" >
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
        <!-- Contenu du bouton "Delete" -->
        <Grid x:Key="pathDelete">
            <Grid Name="deleteBackgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
            <Path Data="M2.8803511,35.504998L25.562742,35.504998 25.461922,35.769711C24.680624,37.899521 24.254,40.199002 24.254,42.596199 24.254,44.993397 24.680624,47.292763 25.461922,49.42239L25.563091,49.688 2.8803511,49.688C1.2905214,49.688,-1.9703777E-07,48.395962,2.2562905E-14,46.807518L2.2562905E-14,38.382981C-1.9703777E-07,36.794033,1.2905214,35.504998,2.8803511,35.504998z M38.677929,34.116001L35.216003,37.578362 40.677326,43.039303 35.216003,48.500141 38.677929,51.962002 44.139351,46.501164 49.600777,51.962002 53.062004,48.500141 47.600876,43.039303 53.062004,37.578362 49.600777,34.116001 44.139351,39.576943z M44.126499,27.625C52.394669,27.625 59.098,34.325222 59.098,42.596249 59.098,50.859478 52.394669,57.564999 44.126499,57.564999 35.858036,57.564999 29.156,50.859478 29.156,42.596249 29.156,34.325222 35.858036,27.625 44.126499,27.625z M2.8803511,17.594002L43.182117,17.594002C44.771912,17.594002,46.061001,18.883038,46.061001,20.471483L46.061001,22.815359 45.653645,22.779068C45.149654,22.740591 44.640469,22.720999 44.126797,22.720999 37.278542,22.720999 31.227795,26.204043 27.652769,31.491766L27.471674,31.774002 2.8803511,31.774002C1.2905214,31.774002,-1.9703777E-07,30.484967,2.2562905E-14,28.893622L2.2562905E-14,20.471483C-1.9703777E-07,18.883038,1.2905214,17.594002,2.8803511,17.594002z M8.4794798,3.8200004C6.6786065,3.82 5.211,5.2858002 5.2109985,7.0880244 5.211,8.8926909 6.6786065,10.356001 8.4794798,10.356001 10.281624,10.356001 11.748,8.8926909 11.748,7.0880244 11.748,5.2858002 10.281624,3.82 8.4794798,3.8200004z M2.8803511,0L43.182117,0C44.771912,1.7200194E-07,46.061001,1.2890874,46.061001,2.8775013L46.061001,11.302039C46.061001,12.893373,44.771912,14.18,43.182117,14.180001L2.8803511,14.180001C1.2905214,14.18,-1.9703777E-07,12.893373,2.2562905E-14,11.302039L2.2562905E-14,2.8775013C-1.9703777E-07,1.2890874,1.2905214,1.7200194E-07,2.8803511,0z" 
                  Stretch="Uniform" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                  Style="{DynamicResource ResourceKey=pathButtonStyle}">
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
        <!-- Contenu du bouton "Exit" -->
        <Grid x:Key="pathExit">
            <Grid Name="exitBackgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
            <Path Data="M34.201,35.752792L36.154999,37.072937 36.154999,61.028999 26.27,61.028999 26.27,58.465 34.201,58.465z M18.94405,30.222998C17.962244,30.222998 17.167,31.018967 17.167,32.00045 17.167,32.982033 17.962244,33.778 18.94405,33.778 19.925556,33.778 20.721001,32.982033 20.721001,32.00045 20.721001,31.018967 19.925556,30.222998 18.94405,30.222998z M44.257435,25.653L44.257435,29.183289 51.842999,29.183289 51.842999,35.705292 44.257435,35.705292 44.257435,39.237999 34.201,32.444984z M26.27,3.8619995L36.154999,3.8619995 36.154999,27.81745 34.201,29.137484 34.201,6.2989998 26.27,6.2989998z M24.27,0L24.27,64 0,60 0,4.0000029z" 
                  Stretch="Uniform" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                  Style="{DynamicResource ResourceKey=pathButtonStyle}">
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
        <!-- Contenu du bouton "Save" -->
        <Grid x:Key="pathNext">
            <Grid Name="nextBackgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
            <Path Data="F1M1568.35,1934.42L1551.46,1917.44 1551.46,1925.43C1517.67,1925.43 1515.02,1945.4 1515.02,1949.39 1522.31,1933.42 1551.46,1941.4 1551.46,1941.4L1551.46,1949.39 1568.35,1934.42z" 
                  Stretch="Uniform" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                  Style="{DynamicResource ResourceKey=pathButtonStyle}">
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
        <!-- Contenu du bouton "Save" -->
        <Grid x:Key="pathSave">
            <Grid Name="savebackgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
            <Path Data="M8.1099597,36.94997L8.1099597,41.793968 39.213959,41.793968 39.213959,36.94997z M12.42,0.049999889L18.4,0.049999889 18.4,12.252 12.42,12.252z M0,0L7.9001866,0 7.9001866,14.64218 39.210766,14.64218 39.210766,0 47.401001,0 47.401001,47.917 0,47.917z" 
                  Stretch="Uniform" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                  Style="{DynamicResource ResourceKey=pathButtonStyle}">
                <Path.RenderTransform>
                    <TransformGroup>
                        <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </TransformGroup.Children>
                    </TransformGroup>
                </Path.RenderTransform>
            </Path>
        </Grid>
    </ResourceDictionary>
</Application.Resources>

我这样消费它们:

<telerik:RadButton x:Name="rbCancel"
                               MinWidth="48" MinHeight="48" 
                               Style="{DynamicResource ResourceKey=cancelButtonStyle}"/>

我这样打开对话框:

wpfMaintenanceInspectionType wnd = new wpfMaintenanceInspectionType();
        wnd.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        wnd.ShowDialog();
        wnd = null;

所以,我第一次显示对话框时,一切都很好,但下一次,pathButtonStyle不再工作。当我启用时,鼠标悬停在按钮上,不会发生任何事情

我的代码出了什么问题?

---更新1-

如果我将样式放在ResourceDictionary文件(ButtonStyles.xaml)中,并将其添加到他的ctor中的对话框中,那么每一个都可以很好地工作。

ResourceDictionary dic = new ResourceDictionary();
        Uri u = new Uri("pack://application:,,,/BuildingInspector;component/Resources/ButtonStyles.xaml", UriKind.Absolute);
        dic.Source = u;
        this.Resources.MergedDictionaries.Add(dic);

为什么它不适用于app.xaml中定义的样式?

---更新2---对话框是从基类appClass:CBIBaseRadWindow构建的基类继承自RadWindow public partial class CBIBaseRadWindow : RadWindow

---更新3---我尝试了一件新的事情:我创建了一个新的对话框,只有一个按钮,点击按钮就会创建这个新对话框的新实例。结果:

一开始,第一个实例运行良好,图像出现,触发器(IsPressed、IsMouseOver)运行良好我一点击按钮,就会出现一个带有图像的新对话框,但是:-触发器对此对话框无效-第一个实例上的图像消失-第一个实例上的触发器应用于第二个实例(如果我按下第一个实例的按钮,第二个例子上的颜色变化)

这就好像该样式无法应用于多个实例??

---更新4---WPF中依赖属性的内部看完这篇文章后,我认为问题出在我的依赖属性上。它们以全局风格定义:

<Style x:Key="cancelButtonStyle" 
               BasedOn="{StaticResource ResourceKey=defaultButtonStyle}" 
               TargetType="{x:Type telerik:RadButton}">
            <Setter Property="app:DependencyProperties.ImagePath" Value="{DynamicResource ResourceKey=pathCancel}" />
            <Setter Property="app:DependencyProperties.Color" Value="Salmon" />
            <Setter Property="app:DependencyProperties.MouseOverColor" Value="SandyBrown" />
            <Setter Property="app:DependencyProperties.PressedColor" Value="Red" />
        </Style>

正因为如此,也许第二个实例一创建,第一个实例就失去了一些东西。

有人能证实吗?

感谢

WPF 4.0全球风格问题

我认为您不想在取消按钮样式中使用DynamicResource。将DynamicResource更改为StaticResource

看看这个堆栈溢出问题,了解更多信息:

什么';WPF中StaticResource和DynamicResource之间的区别是什么?