天天看點

WPF中一個控件綁定另一個控件的屬性

原文:

WPF中一個控件綁定另一個控件的屬性

如同一個Grid中的一個按鈕根據另一個按鈕的顯示與否作出不同的響應:

綁定的時候通過ElementName來指定控件

<Grid Margin="50,130">

  <Grid.ColumnDefinitions>

    <ColumnDefinition/>

    <ColumnDefinition Width="40"/>

  </Grid.ColumnDefinitions>

  <Button Content="asdfasdfadsfa" Background="Aquamarine">

    <Button.Style>

      <Style TargetType="{x:Type Button}">

        <Setter Property="Template">

          <Setter.Value>

            <ControlTemplate TargetType="{x:Type Button}">

              <Border x:Name="bd" BorderBrush="Black" Background="{TemplateBinding Background}" BorderThickness="1" CornerRadius="6,0,0,6" >

                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Content}"/>

            </Border>

          <ControlTemplate.Triggers>

            <DataTrigger Binding="{Binding ElementName=btn2,Path=Visibility}" Value="Collapsed">

              <Setter TargetName="bd" Property="CornerRadius" Value="6,6,6,6"/>

            </DataTrigger>

          </ControlTemplate.Triggers>

        </ControlTemplate>

      </Setter.Value>

    </Setter>

  </Style>

</Button.Style>

  </Button>

    <Button  x:Name="btn2" Grid.Column="2" Command="{Binding MainCommand}" Background="Beige">

      <Button.Style>

        <Style TargetType="{x:Type Button}">

          <Setter Property="Template">

            <Setter.Value>

              <ControlTemplate TargetType="{x:Type Button}">

                <Border BorderBrush="Black" Background="{TemplateBinding Background}" BorderThickness="0,1,1,1" CornerRadius="0,6,6,0" >

                  <Path Data="M 0,0 L 20,0 L 10,10 Z" Fill="Black" VerticalAlignment="Center" HorizontalAlignment="Center"/>

                </Border>

              </ControlTemplate>

            </Setter.Value>

          </Setter>

        </Style>

      </Button.Style>

    </Button>

</Grid>