将字典值绑定到UserControl WPF的依赖属性

时间:2012-08-18 20:05:47

标签: c# wpf binding dependency-properties

我正在尝试将设置为tabcontrol的itemsource的Dictionary中的Value绑定到我的TabControl.ContentTemplate中保存的UserControl的DependencyProperty。

对于我的生活,我无法让它绑定,我有一种强烈的感觉,它可能与userControl的“EnvironmentStateView”的DataContext有关,在这个实例中它是一个viewmodel。

在单独的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); }
    }
}

将environmentstateview的DataContext设置为视图模型,但删除这意味着它的datacontext为null,所以我猜它不会继承任何东西。

我的目标是让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中,我猜这是当前的数据上下文。

我故意拼错了与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也许,但我不知道我是否应该大声笑。

1 个答案:

答案 0 :(得分:0)

public static readonly DependencyProperty EnvironmentObjectProperty =
    DependencyProperty.Register(
        "EnvironmentObject",
        typeof(Framework.Environment),
        typeof(EnvironmentStateView),
        new PropertyMetadata(null));

父类不应该是EnvironmentStateView而不是UserControl吗?