尝试将我的ViewModel中的属性绑定到DataTrigger [英] Trying to bind a property in my ViewModel to a DataTrigger

查看:84
本文介绍了尝试将我的ViewModel中的属性绑定到DataTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ViewModel中有一个名为IsEditing的布尔属性,我想基于值为False将某些样式应用于各种元素。

I have a boolean property named IsEditing in my ViewModel and I'm wanting to apply certain styles to various elements based on the value being False.

My View的DataContext是正确链接到我的ViewModel,一切都按预期工作。我也有一些< g class =" gr_ gr_38 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace"数据-GR-ID =" 38"
id =" 38">非< / g> -DataTrigger触发器也按预期工作。但是,当我尝试将IsEditing绑定到DataTrigger时似乎没有任何改变。

My View's DataContext is properly linked to my ViewModel and everything is working as expected. I also have some <g class="gr_ gr_38 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="38" id="38">non</g>-DataTrigger triggers that work as expected too. However, when I try to bind IsEditing to a DataTrigger nothing seems to change.

<Style.Triggers>

    <DataTrigger Binding="{Binding Path=IsEditing" Value="False">

        <Setter Property="Opacity" Value=".15" />

    </DataTrigger>

</Style.Triggers>




我在这里可以缺少什么?谢谢!

What could I be missing here? Thanks!

推荐答案

嗨T Gregory,

Hi T Gregory,

I做一个关于如何使用DataTrigger以及如何在DataTrigger中使用Binding的示例。

I do one sample about how to use DataTrigger and how to use Binding in DataTrigger.

我在窗口中添加了一个Button和TextBox。请不要忘记实施
INotifyPropertyChanged
界面以通知属性已更改。

I add one Button and TextBox in window. Please don't forget to implement INotifyPropertyChanged interface to notify property changed.

  <StackPanel>
        <Button
            Name="btn1"
            Width="200"
            Height="30"
            Content="btn1" Click="btn1_Click"/>
        <TextBlock
            Margin="0,20,0,0"
            HorizontalAlignment="Center"
            FontSize="48">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Text" Value="No" />
                    <Setter Property="Foreground" Value="Red" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEditing}" Value="True">
                            <Setter Property="Text" Value="Yes!" />
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </StackPanel>


 public partial class Window19 : Window
    {
        public boolenclass boolclass { get; set; }
        public Window19()
        {
            InitializeComponent();
            boolclass = new boolenclass();
            this.DataContext =boolclass;
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            if(boolclass.IsEditing==false)
            {
                boolclass.IsEditing = true;
            }
            else
            {
                boolclass.IsEditing = false;
            }
            
        }
    }

    public class boolenclass:ViewModelBase
    {
        private Boolean _IsEditing;
        public Boolean IsEditing
        {
            get { return _IsEditing; }
            set
            {
                _IsEditing = value;
                RaisePropertyChanged("IsEditing");
            }
        }
        public boolenclass()
        {
            IsEditing = false;
        }
    }

实施INotifyPropertyChanged:

Implementing INotifyPropertyChanged:

 public class ViewModelBase:INotifyPropertyChanged
    {
        
        public event PropertyChangedEventHandler PropertyChanged;

        
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

顺便说一句,如果您已经解决了以下主题,请记得通过将有用的帖子标记为答案来关闭您的主题,这对其他社区有益遇到同样问题的会员。

By the way, If you have solved your following thread, please remember to close your thread by marking the helpful post as answer, it is beneficial to other community members who face the same issue.

感谢您的理解。

HTTPS? //social.msdn.microsoft.com/Forums/vstudio/en-US/ff3f0128-5448-4d4b-ab6f-2d71a6afb7bc/bringing-a-usercontrol-into-an-existing-project?forum=wpf#eda48411-5440- 4b64-90cc-67966b9deca3

最好的问候,

Cherry





这篇关于尝试将我的ViewModel中的属性绑定到DataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆