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>

posted @ 2017-03-07 18:06  一菲聪天  阅读(11565)  评论(0编辑  收藏  举报