WPF程序设计指南第24张样式中DataTrigger中使用绑定的例子

时间:2023-01-21 00:09:48
 此DataTrigger类型类似Trigger,但是它用Binding取代Property  Binding通常

引用到另一个elemente  DataTrigger可以设定Binging的值,如下面的文件所示。
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Control.FontSize" Value="18"/>
            <Setter Property="Control.HorizontalAlignment" Value="Center"/>
            <Setter Property="Control.Margin" Value="10"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=txtBox,Path=Text.Length}" Value="0">
                    <Setter Property="Button.IsEnabled" Value="False"/>
                </DataTrigger>
                  
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <TextBox Name="txtBox" HorizontalAlignment="Center" Width="2in" Margin="5"/>
    <Button>Buttont No.1</Button>
</StackPanel>
  此文件DataTrigger包含到Textbox element的绑定,引用到txtbox的名称。Path是
Text.Length,指的是此TextBox的text Property的Length property。当此值为0,此Button
的Enabled property会被设定为false,这么一来,只有当Textbox内有文字时,此按钮才
会被enable。通常只有当某些条件为True,对话框才会让按钮enable,所以此处相当适合
使用DataTrigger