我正试图将Dictionary中的一个Value绑定到我的tabcontrol.ContentTemplate.中的UserControl的DependencyProperty
就我而言,我无法将其绑定,我有一种强烈的感觉,这可能与用户控件"EnvironmentStateView"的DataContext有关,在本例中,它是一个视图模型。
在一个单独的TextBlock中,字典键的绑定适用于集合中的每个项目,但不幸的是,仅此而已
环境集合视图.xaml
<TabControl Name="EnvironmentCollectionTabControl" ItemsSource="{Binding environmentCollection}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Key}"></TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<local:EnvironmentStateView Grid.Column="0" Grid.Row="0" EnvironmentObject="{Binding Value}" EnvironmentKey="{Binding Key}"></local:EnvironmentStateView>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
EnvironmentStateView.xaml
/// <summary>
/// Interaction logic for EnvironmentStateView.xaml
/// </summary>
[Export(typeof(EnvironmentStateView))]
public partial class EnvironmentStateView : UserControl
{
/// <summary>
/// Environment object dependency property
/// </summary>
public static readonly DependencyProperty EnvironmentObjectProperty =
DependencyProperty.Register(
"EnvironmentObject",
typeof(Framework.Environment),
typeof(EnvironmentStateView),
new PropertyMetadata(null));
/// <summary>
/// Environment key dependency property
/// </summary>
public static readonly DependencyProperty EnvironmentKeyProperty =
DependencyProperty.Register(
"EnvironmentKey",
typeof(string),
typeof(EnvironmentStateView),
new PropertyMetadata(null));
[ImportingConstructor]
public EnvironmentStateView()
{
// Set data context
this.DataContext = new EnvironmentStateViewModel();
// Initialise
InitializeComponent();
}
/// <summary>
/// Gets or sets the environment object
/// </summary>
public Framework.Environment EnvironmentObject
{
get { return (Framework.Environment)GetValue(EnvironmentObjectProperty); }
set { SetValue(EnvironmentObjectProperty, value); }
}
/// <summary>
/// Gets or sets the environment key
/// </summary>
public string EnvironmentKey
{
get { return (string)GetValue(EnvironmentObjectProperty); }
set { SetValue(EnvironmentObjectProperty, value); }
}
}
environmentstateview的DataContext被设置为视图模型,尽管删除它意味着它的DataContext为null,所以我猜它一开始并没有继承任何东西。
我的目标是让environmentCollectionView在其视图模型中使用environmentCollection Observable字典,将其作为项源绑定到选项卡,然后将集合中的各个环境对象传递给单独的用户控件,以处理与环境对象本身相关联的特定视图/视觉效果。
我认为我能做到这一点的唯一方法是将dictionary Value作为依赖属性传递,这样每个用户控件都可以做自己的事情:),当然这可能是一种糟糕的方法。
感谢提供的任何帮助
问候Wolfe
EDIT*已将UserControl更改为EnvironmentStateView
编辑*在修复之前的错误后,我注意到上出现了更多的绑定错误信息
System.Windows.Data Error: 40 : BindingExpression path error: 'Key' property not found on 'object' ''EnvironmentStateViewModel' (HashCode=63276897)'. BindingExpression:Path=Key; DataItem='EnvironmentStateViewModel' (HashCode=63276897); target element is 'EnvironmentStateView' (Name=''); target property is 'EnvironmentKey' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Value' property not found on 'object' ''EnvironmentStateViewModel' (HashCode=32159097)'. BindingExpression:Path=Value; DataItem='EnvironmentStateViewModel' (HashCode=32159097); target element is 'EnvironmentStateView' (Name=''); target property is 'EnvironmentObject' (type 'Environment')
现在Key和Value显然不在EnvironmentStateViewModel中,我想这就是当前的数据上下文。
我故意拼错了绑定到Keyy的EnvironmentTagTextBlock,这给了我绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'Keyy' property not found on 'object' ''KeyValuePair`2' (HashCode=-459631492)'. BindingExpression:Path=Keyy; DataItem='KeyValuePair`2' (HashCode=-459631492); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
所以我的猜测是,我需要以某种方式将我的DataItem从EnvironmentStateViewModel更改为KeyValuePair`2可能,但我不知道我应该如何或是否应该哈哈大笑。
public static readonly DependencyProperty EnvironmentObjectProperty =
DependencyProperty.Register(
"EnvironmentObject",
typeof(Framework.Environment),
typeof(EnvironmentStateView),
new PropertyMetadata(null));
父类不应该是EnvironmentStateView
而不是UserControl
吗?