简体   繁体   English

在setter中使用绑定属性

[英]Using a bound property in a setter

I have a button which displays three lines of text using a StackPanel and three TextBlocks. 我有一个按钮,该按钮使用StackPanel和三个TextBlocks显示三行文本。

The visibility of each textblock are using the same binding as follows: 每个文本块的可见性都使用相同的绑定,如下所示:

<TextBlock Name="bF12Tl1" 
           Text="Line1"
           Visibility="{Binding F12ShowText}"
           HorizontalAlignment="Center"/>

So far I have been able to use bindings in Triggers, but not in Setters. 到目前为止,我已经能够在触发器中使用绑定,但不能在设置器中使用。 Is it possible to use this binding to change the visibility of all three TextBlocks in a Setter like the one below? 是否可以使用此绑定来更改Setter中所有三个TextBlock的可见性,如下所示?

<Trigger Property="IsEnabled" Value="False">
    <Setter Property="BorderThickness" Value="4"/>
    <Setter Property="BorderBrush" Value="#737373"/>
    <Setter Property="Background" Value="Gray"/>
    //(Use DataBinding F12ShowText to change visibility here)
</Trigger>

Thank you 谢谢

Something as simple as this may help 这样简单的事情可能会有所帮助

 <Button x:Name="MyButton"/>
    <Style  TargetType=TextBlock>
      <Style.Triggers>
         <DataTrigger Binding="{Binding ElementName= MyButton, Path=IsEnabled}" Value="false>
            <Setter Property="Visibility" Value="Hidden/>
         </DataTrigger>
      </Style.Triggers>
    </Style>

Please note I wrote it in notepad, as I dont have VS running atm, so there maybe " or bracket missing here or there. But I don't see a reason why this would not work. 请注意,由于我没有在VS上运行atm,所以我在记事本中写了它,所以这里或那里可能缺少“或”括号。但是我看不出为什么这行不通。

It was much simpler than i believed it to be, I did not need to use the DataBinding at all. 它比我想象的要简单得多,我根本不需要使用DataBinding。

Using the Name of each TextBlock along with "TargetName" allows me to easily change the property of items not directly related to the buttons. 通过将每个TextBlock的名称与“ TargetName”一起使用,可以轻松更改与按钮不直接相关的项目的属性。

<Trigger Property="IsEnabled" Value="False">
        <Setter Property="BorderThickness" Value="4"/>
        <Setter Property="BorderBrush" Value="#737373"/>
        <Setter Property="Background" Value="Gray"/>
        <Setter TargetName="bF12Tl1" Property="Visibility" Value="Hidden"/>
        <Setter TargetName="bF12Tl2" Property="Visibility" Value="Hidden"/>
        <Setter TargetName="bF12Tl3" Property="Visibility" Value="Hidden"/>
</Trigger>

If anybody have an opinion if this is a good solution or not, please let me know. 如果有人对这是否是一个好的解决方案有意见,请告诉我。

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

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