【发布时间】:2016-09-16 10:53:23
【问题描述】:
我创建了一个UserControl,并在控件中绑定了多个自定义 DependencyProperties。但由于某种原因,其中一个因错误而失败:
System.Windows.Data 错误:4:找不到绑定源 参考“元素名称=YearSelectorControl”。 BindingExpression:Path=SelectedBorderColor;数据项=空;目标 元素是“SolidColorBrush”(HashCode=45568629);目标属性是 “颜色”(输入“颜色”)
该控件包括ToggleButton 和Popup。通过单击ToggleButton 打开Popup 时会出现错误。
这里是 xaml:
<UserControl x:Class="MyProject.Views.YearSelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyProject.Views"
mc:Ignorable="d"
d:DesignHeight="23" d:DesignWidth="181"
x:Name="YearSelectorControl">
<UserControl.Resources>
<SolidColorBrush x:Key="HoverColorBrush" Color="{Binding ElementName=YearSelectorControl, Path=HoverColor}"/> <!--This binding is virtually identical, but works fine-->
<SolidColorBrush x:Key="SelectedBorderColorBrush" Color="{Binding ElementName=YearSelectorControl, Path=SelectedBorderColor}"/> <!--This is the binding that fails-->
<local:YearToBackColorConverter x:Key="YearToBackColorConverter"/>
<local:YearToBorderThicknessConverter x:Key="YearToBorderThicknessConverter"/>
<Style x:Key="ForwardBackButtons" TargetType="{x:Type Button}" >
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
<ControlTemplate x:Key="YearButton" TargetType="{x:Type Button}">
<Grid Width="55" Height="30" Margin="2">
<Rectangle x:Name="ButtonRectangle" Stroke="{StaticResource SelectedBorderColorBrush}"> <!--Use of the failing brush-->
<!--Removed code that determines StrokeThickness. Works correctly if Stroke set to a specific color rather than a Static Resource-->
</Rectangle>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonRectangle" Property="Fill" Value="{StaticResource HoverColorBrush}"/> <!--Use of the properly binding brush-->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<!--Layout here-->
</Grid>
</UserControl>
这是绑定到SolidColorBrush 资源的两个DependencyProperties 的代码隐藏摘录:
public Color HoverColor
{
get { return (Color)GetValue(HoverColorProperty); }
set { SetValue(HoverColorProperty, value); }
}
public static readonly DependencyProperty HoverColorProperty =
DependencyProperty.Register("HoverColor", typeof(Color), typeof(YearSelector), new PropertyMetadata(Color.FromArgb(255,190,230,253)));
public Color SelectedBorderColor
{
get { return (Color)GetValue(SelectedBorderColorProperty); }
set { SetValue(SelectedBorderColorProperty, value); }
}
public static readonly DependencyProperty SelectedBorderColorProperty =
DependencyProperty.Register("SelectedBorderColor", typeof(Color), typeof(YearSelector), new PropertyMetadata(Color.FromArgb(255,126,0,234)));
【问题讨论】:
-
我认为你必须使用路径:
DataContext.SelectedBorderColor. -
这是因为弹出窗口位于所有其他元素的不同视觉树中。 Check this for solution
-
谢谢@Sinatr。我试过了,但这不起作用。
-
PopUp只有一个按钮吗?无法理解发生错误的原因 -
@Bolu:我查看了那个解决方案,但我不确定如何在这里实现它。绑定不会发生在
Popup,而是发生在UserControl.Resources。
标签: c# wpf binding stroke solidcolorbrush