视频图像处理系列索引 || Arcgis/Engine/Server开发索引 || Web Map Gis开发索引 || jquery表格组件 JQGrid索引
WPF MVVM模式开发实现简明教程索引 || ArcGIS Runtime WPF(.net C#)开发简明教程索引

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="..\Style\MultiValueConverterResource.xaml"/>
                <ResourceDictionary  Source="..\Style\ButtonStyle.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;

  

运行效果





其他绑定都是一样的,不再多说

 

posted @ 2020-12-17 17:20  jhlong  阅读(314)  评论(0编辑  收藏  举报
海龙的博客 jhlong@cnblogs 版权所有© 转载请注明链接.有用请推荐一下
代码全部经过本人测试,但不保证复制粘贴就正常运行,更不保证能解决你的问题,请结合前后代码及描述理解后修改和使用