gpt4 book ai didi

WPF - IsEnabled 绑定(bind)到 DependencyProperty 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 00:06:56 25 4
gpt4 key购买 nike

我在我的窗口中定义了一个依赖属性,如下所示:

public static readonly DependencyProperty IsGenericUserProperty = DependencyProperty.Register("IsGenericUser", typeof (bool), typeof (MainWindow));
public bool IsGenericUser
{
get { return (bool) GetValue(IsGenericUserProperty); }
set { SetValue(IsGenericUserProperty, value); }
}

在我的窗口的构造函数中,我设置了持有按钮的容器的数据上下文:
QuickListButtonsStackPanel.DataContext = this;

我将依赖属性绑定(bind)到按钮的 IsEnabled 属性:
<Button IsEnabled="{Binding IsGenericUser}" .../>

启动时 IsGenericUser 为 true,因此该按钮已启用。当我将 IsGenericUser 设置为 false 时,该按钮将被禁用。但是,如果我再次将 IsGenericUser 设为 true,则按钮不会发生任何事情,并且它仍然处于禁用状态。
我究竟做错了什么?

谢谢!

编辑:
这是我与按钮一起使用的样式。这种样式导致了问题(如果按钮没有自定义样式,它可以正常工作):
<Style x:Key="BlackButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOverActivating">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2F2F2F"/>
<SplineColorKeyFrame KeyTime="00:00:00.1270000" Value="#FF2391FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MouseOverDeactivating">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2391FF"/>
<SplineColorKeyFrame KeyTime="00:00:00.2200000" Value="#FF2F2F2F"/>

</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="PressActivating">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2391FF"/>
<SplineColorKeyFrame KeyTime="00:00:00.1370000" Value="#FF48D6FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="PressedDeactivating" FillBehavior="Stop" >
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF48D6FF"/>
<SplineColorKeyFrame KeyTime="00:00:00.2370000" Value="#FF2391FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="DisableActivating">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FFA7A7A7"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid>
<Rectangle Stroke="Transparent" RadiusX="5" RadiusY="5" x:Name="rectangle">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF2F2F2F" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" OpacityMask="{x:Null}"/>
<Rectangle Stroke="Transparent" RadiusX="5" RadiusY="5" x:Name="WhiteGlow">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#5BFFFFFF" Offset="0"/>
<GradientStop Color="#00FFFFFF" Offset="0.5"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsCancel" Value="False"/>
<EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
<Trigger Property="IsFocused" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverActivating}" x:Name="MouseOverActivating_BeginStoryboard2"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverActivating}" x:Name="MouseOverActivating_BeginStoryboard1"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverDeactivating}" x:Name="MouseOverDeactivating_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverActivating}" x:Name="MouseOverActivating_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="PressActivating_BeginStoryboard" Storyboard="{StaticResource PressActivating}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard x:Name="PressedDeactivating_BeginStoryboard" Storyboard="{StaticResource PressedDeactivating}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource DisableActivating}" x:Name="DisableActivating_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

最佳答案

您如何将属性设置为 False/True?如果我按原样复制您的代码,它会完美运行。必须有其他一些您可能不希望对其产生影响的事情发生,例如按钮上的动画或清除绑定(bind)的事情。您是否可以发布更多代码,以帮助阐明可能执行此操作的内容?

这也是我测试的代码:

<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300">
<Grid>
<StackPanel x:Name="QuickListButtonsStackPanel">
<Button IsEnabled="{Binding IsGenericUser}"
Content="Bound Button" />
<Button Content="Change Binding"
Click="Button_Click" />
</StackPanel>
</Grid>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
QuickListButtonsStackPanel.DataContext = this;
}
public static readonly DependencyProperty IsGenericUserProperty =
DependencyProperty.Register(
"IsGenericUser",
typeof(bool),
typeof(Window1));

public bool IsGenericUser
{
get { return (bool)GetValue(IsGenericUserProperty); }
set { SetValue(IsGenericUserProperty, value); }
}

private void Button_Click(object sender, RoutedEventArgs e)
{
IsGenericUser = !IsGenericUser;
}
}

编辑:
您也可以添加一个文本框以查看它是否有效,
<Button x:Name="uiButton"
IsEnabled="{Binding IsGenericUser}"
Style="{StaticResource BlackButtonStyle}"
Content="Bound Button"/>
<TextBlock Text="{Binding ElementName=uiButton, Path=IsEnabled}" />

看起来问题只是样式的 Storyboard ,如果您添加它,它是否仍然显示 IsEnabled 不应该是错误的?

关于WPF - IsEnabled 绑定(bind)到 DependencyProperty 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com