绑定 IsEnabled 不起作用
Binding IsEnabled not Working
我的按钮的 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,按钮总是启用的。
我用谷歌搜索了几个小时,...但我什么也没找到:-(
感谢帮助!
因为您要替换 Button 的模板,所以您需要在自定义控件模板中处理 IsEnabled。
<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"
IsEnabled="{TemplateBinding IsEnabled}">
<Grid>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
您正在覆盖控件的模板,因此默认模板(应用禁用的外观)不再存在。
您需要根据需要重新应用禁用的外观。
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 中自己处理它,例如
我的按钮的 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,按钮总是启用的。 我用谷歌搜索了几个小时,...但我什么也没找到:-(
感谢帮助!
因为您要替换 Button 的模板,所以您需要在自定义控件模板中处理 IsEnabled。
<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"
IsEnabled="{TemplateBinding IsEnabled}">
<Grid>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
您正在覆盖控件的模板,因此默认模板(应用禁用的外观)不再存在。
您需要根据需要重新应用禁用的外观。
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 中自己处理它,例如