C# 绑定已启用,无法工作
我的按钮的IsEnabled属性有问题 这是我的按钮样式:C# 绑定已启用,无法工作,c#,wpf,button,styles,C#,Wpf,Button,Styles,我的按钮的IsEnabled属性有问题 这是我的按钮样式: <Style x:Key="Button_selectable" TargetType="{x:Type Button}"> <Setter Property="FontSize" Value="15"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter
<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}"/>
如果没有我的系统,IsEnabled属性工作正常。但是如果我使用这个系统,按钮总是启用的。
我在谷歌上搜索了几个小时,。。。但我什么也没发现:-(
谢谢您的帮助!因为您要替换按钮的模板,所以需要在自定义控件模板中处理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>
您正在覆盖控件的模板,因此默认模板(应用禁用外观)不再存在 如果需要,您需要重新应用禁用的外观 如果您需要一些示例代码,可以使用不同的状态。我还复制了下面禁用状态的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中使用其他方法来处理它,比如我已经尝试过了。但是它不起作用。我的绑定属性甚至没有被调用过一次,尽管我使用了OnPropertyChanged。