[英]Binding Dictionary Value to Dependency Property of a UserControl WPF
我试图将一个值作为一个tabcontrol的itemsource从Dictionary集中绑定到一个保存在TabControl.ContentTemplate中的UserControl的DependencyProperty。
在我的一生中,我无法绑定它,我强烈感觉到它可能与userControl'EnvironmentStateView'的DataContext有关,在这种情况下,它是一个视图模型。
在单独的TextBlock中,对字典键的绑定适用于集合中的每个项目,但不幸的是。
EnvironmentCollectionView.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); }
}
}
尽管将其删除意味着它的datacontext为null,所以将environmentstateview的DataContext设置为视图模型,因此我想它不会继承任何东西。
我的目标是让environmentCollectionView在其viewmodel中使用environmentCollection Observable字典,将其作为itemsource绑定到Tabctronol,然后传递集合中的各个环境对象以分离用户控件,以处理与环境对象本身关联的特定视图/视觉效果。
我认为我能做到这一点的唯一方法是将字典值作为依赖项属性传递,以便每个用户控件都能完成其任务:),这可能是一种糟糕的方法。
任何帮助表示赞赏
问候沃尔夫
编辑*将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中,我想它是当前数据上下文。
我故意拼写错误地将EnvironmentTagTextBlock绑定到Keyy,这给了我绑定错误:
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
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.