简体   繁体   English

MVVM中的样式按钮

[英]Styling button in MVVM

I am writing a small WPF application using MVVM pattern. 我正在使用MVVM模式编写一个小型WPF应用程序。

I set some style resources for buttons into my main window and I would like them to apply to the buttons into the views. 我在主窗口中为按钮设置了一些样式资源,并希望将它们应用于视图中的按钮。 The problem is that some of the buttons are having a style with trigger. 问题在于某些按钮具有带有触发器的样式。 So I would like to inherit this style from the generic one 所以我想从通用样式中继承这种样式

here is my main window code: 这是我的主窗口代码:

 <Window.Resources>
    <DataTemplate DataType="{x:Type vm:HomeViewModel}">
        <views:HomeView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:DetailReportViewModel}">
        <views:DetailReportView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:TransferViewModel}">
        <views:TransferView/>
    </DataTemplate>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="MinWidth" Value="30"/>
        <Setter Property="Foreground" Value="Red"/>

    </Style>
    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Margin" Value="5"/>
    </Style>
   </Window.Resources>

here is the button XAML into my view/usercontrol 这是我的视图/用户控件中的按钮XAML

<Button Content="Delete" Command="{Binding DeleteReportCommand}">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Mode}">
                            <DataTrigger.Value>
                                <vm:Mode>Add</vm:Mode>
                            </DataTrigger.Value>
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Mode}">
                            <DataTrigger.Value>
                                <vm:Mode>Edit</vm:Mode>
                            </DataTrigger.Value>
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

I tried to used BasedOn="{StaticResource {x:Type Button} to inherit but it doesn't seems to work (see img) 我尝试使用BasedOn =“ {StaticResource {x:Type Button}来继承,但是它似乎不起作用(请参阅img)

在此处输入图片说明

I tried with a key name but static resource won't find the key name as it's not on the same view. 我尝试使用键名,但是静态资源找不到键名,因为它不在同一视图中。 And BasedOn is not accepting Dynamic resource. 而且BasedOn不接受动态资源。 Anything I am missing? 我有什么想念的吗?

Thank you 谢谢

You can put all the custom styles in a separate resource dictionary file and you can add it in usercontrol.resources. 您可以将所有自定义样式放在单独的资源字典文件中,并将其添加到usercontrol.resources中。

Refer this: http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF 请参阅: http : //www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF

Or you can put all those in app.xaml (Application.Resources). 或者,您可以将所有这些内容放入app.xaml(Application.Resources)中。

Refer this: http://msdn.microsoft.com/en-us/library/system.windows.resourcedictionary.aspx 引用此: http : //msdn.microsoft.com/zh-cn/library/system.windows.resourcedictionary.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM