WPF MVVM模式开发简明实现教程 6 其他绑定

WPF MVVM模式开发实现简明教程 1 开篇简介 

WPF MVVM模式开发实现简明教程 2 初识 INotifyPropertyChanged

WPF MVVM模式开发简明实现教程 3 事件绑定   

WPF MVVM模式开发实现简明教程 3-1 BaseCommand  

WPF MVVM模式开发实现简明教程 4 ViewModelBase  

WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定 

WPF MVVM模式开发简明实现教程 6 其他绑定  

WPF MVVM模式开发简明实现教程 7 DevExpress MVVM  

WPF MVVM模式开发简明实现教程 8 完结 附全部代码  

Style

Style文件夹下新增资源字典文件ButtonStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApp6.Style">
    <Style x:Key="myButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="#3a3a3a"/>
        <Setter Property="Width" Value="50"/>
        <Setter Property="Height" Value="50"/>
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

  

View改成 

<Button Content="{Binding ButtonContent}" x:Name="button1" Style="{Binding ButtonStyle}" >

同时View里增加对应的ResourceDictionary  

<UserControl.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="..StyleMultiValueConverterResource.xaml"/>
                <ResourceDictionary  Source="..StyleButtonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

  

ViewModel增加

private Style buttonStyle;
        public Style ButtonStyle
        {
            get { return buttonStyle; }
            set
            {
                buttonStyle = value;
                OnPropertyChanged("ButtonStyle");
            }
        }

  

程序初始化时赋值

model.ButtonStyle = (Style)this.FindResource("myButtonStyle");

  运行效果

 IsEnabled

View继续增加

<Button Content="{Binding ButtonContent}" x:Name="button1" Style="{Binding ButtonStyle}" IsEnabled="{Binding ButtonIsEnabled}" >

  

ViewModel增加

private bool buttonIsEnabled;

        public bool ButtonIsEnabled
        {
            get { return buttonIsEnabled; }
            set
            {
                buttonIsEnabled = value;
                OnPropertyChanged("ButtonIsEnabled");
            }
        }

  

初始化时改值

model.ButtonIsEnabled = false;

  

运行效果





其他绑定都是一样的,不再多说
原文地址:https://www.cnblogs.com/jhlong/p/14150760.html