我有一個簡單的附加依賴項屬性應該提供一個鼠標顏色爲我的自定義控件:命名空間的問題:附加依賴找不到屬性
public class Ext
{
public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.RegisterAttached("HighlightBrush", typeof(Brush), typeof(Ext), new PropertyMetadata(default(Brush)));
public static void SetHighlightBrush(DependencyObject element, Brush value)
{
element.SetValue(HighlightBrushProperty, value);
}
public static Brush GetHighlightBrush(DependencyObject element)
{
return (Brush) element.GetValue(HighlightBrushProperty);
}
}
我使用它像這樣在我Generic.xaml
:
<Style TargetType="{x:Type local:MyButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{Binding (local:Ext.HighlightBrush), RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但現在我必須在客戶端使用相同的命名空間:
<Window ...
xmlns:local="clr-namespace:Eval.Wpf.AttachedProperty.Controls;assembly=Eval.Wpf.AttachedProperty.Controls">
<StackPanel>
<local:MyButton Background="LightGray" local:Ext.HighlightBrush="DarkOrange" Content=" - Press me - " />
</StackPanel>
</Window>
了變化克命名空間別的東西,像<c:MyButton Background="LightGray" c:Ext.HighlightBrush="DarkOrange" Content=" - Press me - " />
在
System.Windows.Data Error: 40 : BindingExpression path error: '(local:Ext.HighlightBrush)' property not found on 'object' ''MyButton' (Name='')'. BindingExpression:Path=(local:Ext.HighlightBrush); DataItem='MyButton' (Name=''); target element is 'Border' (Name='Border'); target property is 'Background' (type 'Brush')
爲什麼的使用附加屬性不是類型安全的結束了?我該如何解決這個問題?
我記得在某些情況下'{Binding(local:MyClass.MyProperty)}'語法不起作用。現在解決我的問題是明確使用'Binding.Path'屬性,即'{Binding Path =(local:MyClass.MyProperty)}'來代替。雖然你的問題與命名空間前綴有關,但這很奇怪,所以它可能是完全不同的東西。 – Grx70
@ Grx70是的。這是解決方案。你應該發佈它。你有什麼想法是什麼原因?設置/省略'Path ='表達式有什麼區別? – Marcel