gpt4 book ai didi

c# - 绑定(bind) IsEnabled 不起作用

转载 作者:行者123 更新时间:2023-11-30 14:52:16 25 4
gpt4 key购买 nike

我的按钮的 IsEnabled 属性有问题。

这是我的按钮样式:

        <Style x:Key="Button_selectable" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="15"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="Black" Margin="1,1,1,1">
<Grid>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="MediumSlateBlue"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" Value="False">
<Setter Property="Background" Value="white"/>
</DataTrigger>
</Style.Triggers>
</Style>

用法:

<Button Command="{Binding command}" IsEnabled="{Binding Enable}" x:Name="button" Content="Button1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontSize="13.333" Style="{StaticResource Button_selectable}" Tag="{Binding State}"/>

没有我的 Sytle,IsEnabled 属性工作正常。但是如果我使用这个 Sytle,按钮总是启用的。我用谷歌搜索了几个小时,...但我什么也没找到 :-(

感谢您的帮助!

最佳答案

您正在覆盖控件的模板,因此默认模板(应用禁用外观)不再存在。

您需要根据需要重新应用禁用的外观。

MSDN has an example of a full Button ControlTemplate如果您想要一些示例代码,请使用不同的状态。我还为下面的禁用状态复制了 XAML 的相关位作为示例。

<ControlTemplate TargetType="Button">
<Border TextBlock.Foreground="{TemplateBinding Foreground}"
x:Name="Border"
CornerRadius="2"
BorderThickness="1">

<VisualStateManager.VisualStateGroups>
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource DisabledControlDarkColor}" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource DisabledForegroundColor}" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
(GradientBrush.GradientStops)[1].(GradientStop.Color)"
Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource DisabledBorderDarkColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True" />
</Border>
</ControlTemplate>

或者,您可以使用其他方法在您的 ControlTemplate 中自己处理它,例如 Glen Thomas suggests in his answer

关于c# - 绑定(bind) IsEnabled 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053113/

25 4 0
文章推荐: c# - 为什么我不能用 List> 实例化 List
文章推荐: c# - 多线程文件压缩
文章推荐: c# - 更改框架源 Wpf