我有一点数据绑定问题。 (我通常......)
这是我在运行时在输出窗口中得到的错误:
System.Windows.Data Error: 39 : BindingExpression path error: 'CurrentKlokke' property not found on 'object' ''UIKlokke' (Name='anaKlokke')'. BindingExpression:Path=CurrentKlokke; DataItem='UIKlokke' (Name='anaKlokke'); target element is 'UIKlokke' (Name='anaKlokke'); target property is 'klokke' (type 'Klokke')
'anaKlokke'是'UIKlokke'的一个实例,是一个代表模拟时钟的用户控件。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Klokketrening"
mc:Ignorable="d"
x:Class="Klokketrening.UIKlokke"
x:Name="UserControl"
DataContext="{Binding RelativeSource={RelativeSource self}}"
d:DesignWidth="400" d:DesignHeight="400">
<UserControl.Resources>
<ControlTemplate x:Key="clockTemplate">
<Grid>
<Grid.Resources>
<local:TimeTilVinkel x:Key="hourToAngle"/>
<local:MinuttTilVinkel x:Key="minuteToAngle"/>
</Grid.Resources>
<Grid.LayoutTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</Grid.LayoutTransform>
<Ellipse Width="108" Height="108" StrokeThickness="3">
<Ellipse.Stroke>
<LinearGradientBrush>
<GradientStop Color="LightBlue" Offset="0" />
<GradientStop Color="DarkBlue" Offset="1" />
</LinearGradientBrush>
</Ellipse.Stroke>
</Ellipse>
<Canvas Width="102" Height="102">
<Ellipse Width="8" Height="8" Fill="Black" Canvas.Top="46" Canvas.Left="46" />
<Rectangle x:Name="HourHand" Canvas.Top="26" Canvas.Left="48" Fill="Black" Width="4" Height="25">
<Rectangle.RenderTransform>
<RotateTransform x:Name="HourHand2" CenterX="2" CenterY="25" Angle="{Binding Converter={StaticResource hourToAngle}}" />
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="MinuteHand" Canvas.Top="16" Canvas.Left="49" Fill="Black" Width="2" Height="35">
<Rectangle.RenderTransform>
<RotateTransform CenterX="1" CenterY="35" Angle="{Binding Converter={StaticResource minuteToAngle}}" />
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Grid>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<Control Template="{StaticResource clockTemplate}" DataContext="{Binding klokke}" />
</Grid>
</UserControl>
和背后的代码:
public partial class UIKlokke
{
public Klokke klokke
{
get {return (Klokke)GetValue(UIKlokke.KlokkeProperty); }
set { SetValue(UIKlokke.KlokkeProperty, value); }
}
public static readonly DependencyProperty KlokkeProperty;
public UIKlokke()
{
this.InitializeComponent();
}
static UIKlokke()
{
UIKlokke.KlokkeProperty = DependencyProperty.Register("klokke", typeof(Klokke), typeof(UIKlokke));
}
}
在我使用此usercontrol的地方,我想将usercontrols的'klokke'属性绑定到'Game'对象的'CurrentKlokke'属性。
<local:UIKlokke x:Name="anaKlokke" Grid.Column="0" Grid.Row="1" klokke="{Binding CurrentKlokke}"/>
CurrentKlokke是一个依赖属性。
public Klokke CurrentKlokke
{
get { return (Klokke)GetValue(Game.CurrentKlokkeProperty); }
set { SetValue(Game.CurrentKlokkeProperty, value); }
}
public static readonly DependencyProperty CurrentKlokkeProperty;
static Game()
{
Game.CurrentKlokkeProperty = DependencyProperty.Register("CurrentKlokke", typeof(Klokke), typeof(Game));
}
我在代码中为包含UIKlokke实例的网格设置了DataContext:
_game = new Game(Game.GameLevel.Easy, 10);
gridGameUI.DataContext = _game;
我将列表框的itemsource绑定到Game对象的另一个属性,它工作正常。
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
尝试从UIKlokke控件中删除Klokke属性,并将anaKlokke对象的DataContext直接绑定到Klokke。
此外,您不需要在usercontrol中的网格内部使用Control。您可以使用UserControls.Template:
<UserControl ... DataContext="No needed its set on the game">
<UserControl.Template>
<ControlTemplate>
<Grid>
<!-- ... -->
</Grid>
</ControlTemplate>
</UserControl.Template>
</UserControl>
这样,DataContext直接设置为UserControl
如果您需要在代码隐藏中访问Klokke,可以使用
var klokke = DataContext as Klokke;
if (klokke == null) return;
// use klokke
答案 1 :(得分:0)
对此不确定,我刚开始使用WPF。但是你不应该在绑定中使用Path
,如下所示:
<local:UIKlokke x:Name="anaKlokke"
Grid.Column="0" Grid.Row="1"
klokke="{Binding Path=CurrentKlokke}"/>