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

时间:2016-02-15 10:29:52

标签: c# wpf xaml datatrigger

我有一个文本框,我想在ViewModel中将一个Clr属性设置为ViewModel中Visibility Changed上的另一个属性。

<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>

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:0)

使用中间人DependencyProperty和右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>

我假设您要在property1中设置property2的值(也是CLR属性),这将最终设置Text TextBox的属性。

答案 1 :(得分:0)

可以有办法做到这一点。将TextBox的Visibility绑定到VM的clr属性,并在clr visibility属性的setter中更改Property1的值

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

并在虚拟机中

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