简体   繁体   中英

Using a bound property in a setter

I have a button which displays three lines of text using a StackPanel and three 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?

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

It was much simpler than i believed it to be, I did not need to use the DataBinding at all.

Using the Name of each TextBlock along with "TargetName" allows me to easily change the property of items not directly related to the buttons.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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