gpt4 book ai didi

wpf - 无法在数据触发器中使用标签的自定义属性

转载 作者:行者123 更新时间:2023-12-04 19:35:20 26 4
gpt4 key购买 nike

我正在使用自定义 TitleBar 制作 WPF 应用程序。它还具有自定义按钮样式。虽然我完美地制作了这两个自定义控件,但是在创建一个调整大小按钮时,它会随着 Windows 状态问题的发生而改变它的标题。自定义控件的 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));

请帮忙解决这个问题

最佳答案

我已经解决了这个问题。
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>

关于wpf - 无法在数据触发器中使用标签的自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220919/

26 4 0