Jus*_*tin 6 wpf button hyperlink disabled-control
我有一个包含超链接的按钮,如下所示:
<Button IsEnabled="False">
<Hyperlink IsEnabled="True">Testing</Hyperlink>
</Button>
Run Code Online (Sandbox Code Playgroud)
我需要启用超链接,但要禁用按钮.我怎样才能做到这一点?
以上只会导致两个控件都被禁用.
我通过创建一个简单的包装元素来解决这个问题IsEnabled父元素的继承链。
框架的默认强制回调检查父IsEnabled值并继承它。该控件设置了一个新的强制回调,它只直接返回值而不检查继承。
public class ResetIsEnabled : ContentControl
{
static ResetIsEnabled()
{
IsEnabledProperty.OverrideMetadata(
typeof(ResetIsEnabled),
new UIPropertyMetadata(
defaultValue: true,
propertyChangedCallback: (_, __) => { },
coerceValueCallback: (_, x) => x));
}
}
Run Code Online (Sandbox Code Playgroud)
在问题的示例中,它将像这样使用:
<Button IsEnabled="False">
<ResetIsEnabled>
<!-- Child elements within ResetIsEnabled have IsEnabled set to true (the default value) -->
<Hyperlink>Testing</Hyperlink>
</ResetIsEnabled>
</Button>
Run Code Online (Sandbox Code Playgroud)
拥有奇怪的控制Hyperlink属性IsEnabled。除了你提到的,即从父级继承完整值之外,还有另一种类似的。
Hyperlink对于已关闭 ( IsEnabled="False") 的特定控件,设置 ( IsEnabled="True") 不会更新Hyperlink属性。解决方案 - 使用相对来源Hyperlink(更多信息)。
为了解决您的问题,我决定这不是解决问题的标准方法。所以我创建了一个Class具有自己的依赖属性的。它有它的属性MyIsEnabled和MyStyle。正如您可能从标题中猜到的,第一个设置其属性IsEnabled并MyStyle需要指定按钮样式,模拟IsEnabled="False"行为。
SimulateDisable Style
<Style x:Key="SimulateDisable" TargetType="{x:Type Button}">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Background" Value="Gainsboro" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="4" BorderThickness="1" BorderBrush="DarkBlue" SnapsToDevicePixels="True">
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
用你的属性定义Button:
<Button Name="MyButton" local:MyClass.MyIsEnabled="False" local:MyClass.MyStyle="{StaticResource SimulateDisable}" Width="100" Height="30" Click="Button_Click">
<Hyperlink IsEnabled="True" Click="Hyperlink_Click">Testing</Hyperlink>
</Button>
Run Code Online (Sandbox Code Playgroud)
Listing of MyClass
public class MyClass : DependencyObject
{
public static readonly DependencyProperty MyIsEnabledProperty;
public static readonly DependencyProperty MyStyleProperty;
#region MyIsEnabled
public static void SetMyIsEnabled(DependencyObject DepObject, bool value)
{
DepObject.SetValue(MyIsEnabledProperty, value);
}
public static bool GetMyIsEnabled(DependencyObject DepObject)
{
return (bool)DepObject.GetValue(MyIsEnabledProperty);
}
#endregion MyIsEnabled
#region MyStyle
public static void SetMyStyle(DependencyObject DepObject, Style value)
{
DepObject.SetValue(MyStyleProperty, value);
}
public static Style GetMyStyle(DependencyObject DepObject)
{
return (Style)DepObject.GetValue(MyStyleProperty);
}
#endregion MyStyle
static MyClass()
{
MyIsEnabledProperty = DependencyProperty.RegisterAttached("MyIsEnabled",
typeof(bool),
typeof(MyClass),
new UIPropertyMetadata(false, OnPropertyChanged));
MyStyleProperty = DependencyProperty.RegisterAttached("MyStyle",
typeof(Style),
typeof(MyClass),
new UIPropertyMetadata(OnPropertyChanged));
}
private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Button MyButton = sender as Button;
bool MyBool = GetMyIsEnabled(MyButton);
if (MyBool == false)
{
MyButton.Style = MyClass.GetMyStyle(MyButton);
}
}
}
Run Code Online (Sandbox Code Playgroud)
加上对于事件的Hyperlink指向e.Handled = true,这样事件接下来就不会发生了。
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hyperlink Click!");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button Click! Don't show it's!");
}
Run Code Online (Sandbox Code Playgroud)
Output

PS 抱歉回复晚了:)。