附加的样式绑定 属性 没有显示
style binding attached property didn't show up
我正在尝试设计一个按钮:当鼠标悬停在按钮上时,按钮内容发生变化。所以我尝试使用附加的 属性 来设计它。
这是我的附件class:
class HotButtonAttached:DependencyObject
{
public static int GetNormalStr(DependencyObject obj)
{
return (int)obj.GetValue(NormalStrProperty);
}
public static void SetNormalStr(DependencyObject obj, int value)
{
obj.SetValue(NormalStrProperty, value);
}
// Using a DependencyProperty as the backing store for NormalStr. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NormalStrProperty =
DependencyProperty.RegisterAttached("NormalStr", typeof(int), typeof(HotButtonAttached), new UIPropertyMetadata(0));
public static int GetHotStr(DependencyObject obj)
{
return (int)obj.GetValue(HotStrProperty);
}
public static void SetHotStr(DependencyObject obj, int value)
{
obj.SetValue(HotStrProperty, value);
}
// Using a DependencyProperty as the backing store for HotStr. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HotStrProperty =
DependencyProperty.RegisterAttached("HotStr", typeof(int), typeof(HotButtonAttached), new UIPropertyMetadata(0));
public static int GetDisable(DependencyObject obj)
{
return (int)obj.GetValue(DisableProperty);
}
public static void SetDisable(DependencyObject obj, int value)
{
obj.SetValue(DisableProperty, value);
}
// Using a DependencyProperty as the backing store for Disable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisableProperty =
DependencyProperty.RegisterAttached("Disable", typeof(int), typeof(HotButtonAttached), new PropertyMetadata(0));
}
我为它设计了一个样式:
<Style x:Key="btnStyle1" TargetType="Button">
<Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.NormalStr, RelativeSource={RelativeSource Self}}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.HotStr,RelativeSource={RelativeSource Self}}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.Disable,RelativeSource={RelativeSource Self}}"/>
</Trigger>
</Style.Triggers>
</Style>
然后我应用样式:
<Button x:Name="Btn" Click="Button_Click" Width="48" Height="48" Margin="5" Style="{DynamicResource btnStyle1}"
local:HotButtonAttached.NormalStr="11111"
local:HotButtonAttached.HotStr="22222"
local:HotButtonAttached.Disable="3333">
</Button>
但问题是按钮没有显示任何内容(悬停时应该显示11111、22222,禁用时显示3333)。
如果我将样式更改为:
<Style x:Key="btnStyle1" TargetType="Button">
<Setter Property="Content" Value="Hi"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Content" Value="Good"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Content" Value="Not good"/>
</Trigger>
</Style.Triggers>
</Style>
现在每个按钮都会正常显示 "Hi",悬停时显示 "good",禁用时显示 "not good"。但我的目标是设计一个具有动态内容的按钮模板。看来我的绑定效果不佳,我不知道如何解决,请帮忙。谢谢。
当您绑定到附加属性(例如 Canvas.Left、Grid.Column 或自定义属性)时,您会将它们括在括号中。在你的情况下改变绑定到以下
Path=(local:HotButtonAttached.HotStr)
我正在尝试设计一个按钮:当鼠标悬停在按钮上时,按钮内容发生变化。所以我尝试使用附加的 属性 来设计它。
这是我的附件class:
class HotButtonAttached:DependencyObject
{
public static int GetNormalStr(DependencyObject obj)
{
return (int)obj.GetValue(NormalStrProperty);
}
public static void SetNormalStr(DependencyObject obj, int value)
{
obj.SetValue(NormalStrProperty, value);
}
// Using a DependencyProperty as the backing store for NormalStr. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NormalStrProperty =
DependencyProperty.RegisterAttached("NormalStr", typeof(int), typeof(HotButtonAttached), new UIPropertyMetadata(0));
public static int GetHotStr(DependencyObject obj)
{
return (int)obj.GetValue(HotStrProperty);
}
public static void SetHotStr(DependencyObject obj, int value)
{
obj.SetValue(HotStrProperty, value);
}
// Using a DependencyProperty as the backing store for HotStr. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HotStrProperty =
DependencyProperty.RegisterAttached("HotStr", typeof(int), typeof(HotButtonAttached), new UIPropertyMetadata(0));
public static int GetDisable(DependencyObject obj)
{
return (int)obj.GetValue(DisableProperty);
}
public static void SetDisable(DependencyObject obj, int value)
{
obj.SetValue(DisableProperty, value);
}
// Using a DependencyProperty as the backing store for Disable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisableProperty =
DependencyProperty.RegisterAttached("Disable", typeof(int), typeof(HotButtonAttached), new PropertyMetadata(0));
}
我为它设计了一个样式:
<Style x:Key="btnStyle1" TargetType="Button">
<Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.NormalStr, RelativeSource={RelativeSource Self}}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.HotStr,RelativeSource={RelativeSource Self}}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Content" Value="{Binding Path=local:HotButtonAttached.Disable,RelativeSource={RelativeSource Self}}"/>
</Trigger>
</Style.Triggers>
</Style>
然后我应用样式:
<Button x:Name="Btn" Click="Button_Click" Width="48" Height="48" Margin="5" Style="{DynamicResource btnStyle1}"
local:HotButtonAttached.NormalStr="11111"
local:HotButtonAttached.HotStr="22222"
local:HotButtonAttached.Disable="3333">
</Button>
但问题是按钮没有显示任何内容(悬停时应该显示11111、22222,禁用时显示3333)。
如果我将样式更改为:
<Style x:Key="btnStyle1" TargetType="Button">
<Setter Property="Content" Value="Hi"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Content" Value="Good"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Content" Value="Not good"/>
</Trigger>
</Style.Triggers>
</Style>
现在每个按钮都会正常显示 "Hi",悬停时显示 "good",禁用时显示 "not good"。但我的目标是设计一个具有动态内容的按钮模板。看来我的绑定效果不佳,我不知道如何解决,请帮忙。谢谢。
当您绑定到附加属性(例如 Canvas.Left、Grid.Column 或自定义属性)时,您会将它们括在括号中。在你的情况下改变绑定到以下
Path=(local:HotButtonAttached.HotStr)