简体   繁体   English

WPF:在触发器中设置Clr属性

[英]Wpf: Set Clr property in a Trigger

I have a textbox and i want to set a Clr property from ViewModel to another property in ViewModel on Visibility Changed. 我有一个文本框,我想将Viribility Changed上的Clr属性从ViewModel设置为ViewModel中的另一个属性。

<TextBox Text="{Binding Property1}">
<TextBox.Style>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
       <Style.Triggers>
       <Trigger Property="Visbility" Value="Collapsed">
          <Trigger.Setters>
             ///Set Property1 = Property2
          </Trigger.Setters>
       </Trigger>
</Style>
</TextBox>

Is there any way to do this? 有什么办法吗?

Use an intermediary DependencyProperty and Right BindingMode Combination to achieve your desired Result : 使用中间的DependencyProperty和Right BindingMode组合来获得所需的结果:

 <TextBox Text="{Binding Property1}">
            <TextBox.Style>
                <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                    <Style.Triggers>
                        <Trigger Property="Visbility" Value="Collapsed">
                            <Trigger.Setters>
                                <Setter Property="Tag" Value="{Binding Property2}" />
                                <Setter Property="Tag" Value="{Binding Mode=OneWayToSource,Path=property1}" />
                            </Trigger.Setters>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

I'm assuming that you want to set the value of property2(which is also a CLR property) in property1, which will ultimately set the Text Property of TextBox . 我假设您要在property1中设置property2(也是CLR属性)的值,这最终将设置TextBoxText属性。

There can be a way to do this. 有一种方法可以做到这一点。 You bind the Visibility of the TextBox to a clr property of your VM and in the setter of the clr visibility property change the value of Property1 您将TextBox的Visibility绑定到VM的clr属性,并在clr可见性属性的设置器中更改Property1的值

<TextBox Text="{Binding Property1}" Visibility="{Binding Visibility, Converter={StaticResource ResourceKey=boolConvertor}}"></TextBox>

and in the VM 并在虚拟机中

  public bool Visibility
    {
        get { return visibility; }
        set
        {
            visibility = value;
            if (visibility == false)
            {
                Property1 = Property2;
            }
            OnPropertyChanged();
        }
    }

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

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