无法在数据触发器中使用标签的Custom属性

时间:2017-04-05 02:46:53

标签: wpf xaml custom-controls datatrigger custom-properties

我正在使用自定义TitleBar制作WPF应用程序。它还具有自定义按钮样式。虽然我完美地完成了两个自定义控件,但是在创建一个调整大小按钮时会改变它的标题,因为窗口状态问题会出现。自定义控件的XAML代码是......

<local:MaterialButton Grid.Column="7" BackStyle="{StaticResource StyleWhite}">
  <local:MaterialButton.Triggers>
    <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="Normal">
      <Setter Property="Caption" Value="&#xE922;" />
    </DataTrigger>
    <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"  Value="Maximized">
      <Setter Property="Caption" Value="&#xE923;" />
    </DataTrigger>
  </local:MaterialButton.Triggers>
</local:MaterialButton>

后面的代码是......

        public string Caption
    {   get { return (string)GetValue(CaptionProperty); }
        set { SetValue(CaptionProperty, value); }
    }
    public static readonly DependencyProperty CaptionProperty = 
        DependencyProperty.Register("Caption", typeof(string), typeof(MaterialButton));

请帮助解决此问题

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。 XAML是......

<Style TargetType="{x:Type local:TitleBar}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:TitleBar}">
        <Grid Name="TitleBar" Height="60" HorizontalAlignment="Stretch"
              Background="{Binding Background, FallbackValue=SteelBlue}">
          <Path Fill="#FF2A5478" Style="{Binding TitleBarStyle, FallbackValue='{StaticResource StyleDesign01}'}" 
                HorizontalAlignment="Right" VerticalAlignment="Top" Height="200"/>
            <Grid Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top">
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="30"/>
                  <ColumnDefinition Width="1*"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Name="Icon" Stretch="Uniform" Height="auto" Width="auto" Margin="5"
                Source="{Binding Path=Icon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Column="1" Name="Title" Foreground="White" Margin="5,0,0,0" FontFamily="Segoe UI"
                           Text="{Binding Path=Title, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                           HorizontalAlignment="Left" VerticalAlignment="Center"/>
               <local:FlatButton Grid.Column="8" x:Name="CloseButton" Caption="&#xE947;"
                                 Height="30" Width="45" 
                                 BackStyle="{StaticResource StyleRed}"/>
               <local:FlatButton Grid.Column="7" x:Name="ResizeButton" 
                                 Height="30" Width="45"
                                 BackStyle="{StaticResource StyleWhite}">
               <local:FlatButton.Style>
                  <Style TargetType="local:FlatButton">
                     <Style.Triggers>
                        <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="Normal">
                           <Setter Property="Caption" Value="&#xE922;" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"  Value="Maximized">
                            <Setter Property="Caption" Value="&#xE923;" />
                        </DataTrigger>
                      </Style.Triggers>
                    </Style>
                  </local:FlatButton.Style>
                </local:FlatButton>
                <local:FlatButton Grid.Column="6" x:Name="MinimizeBtton" Caption="&#xE921;"
                                  Height="30" Width="45"
                                  BackStyle="{StaticResource StyleWhite}"/>
              </Grid>
            </Grid>
          </ControlTemplate>
       </Setter.Value>
    </Setter>
 </Style>