【问题标题】:Binding Dictionary Value to Dependency Property of a UserControl WPF将字典值绑定到 UserControl WPF 的依赖属性
【发布时间】:2012-08-18 20:05:47
【问题描述】:

我正在尝试将字典中的值绑定为 tabcontrol 的 itemsource 到我的 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); }
    }
}

environmentstateview 的 DataContext 设置为视图模型,尽管删除它意味着它的 datacontext 为空,所以我猜它一开始就没有继承任何东西。

我的目标是让 environmentCollectionView 在其视图模型中使用 environmentCollection Observable 字典,将其作为 itemsource 绑定到 tabctronol,然后将集合中的各个环境对象传递给单独的用户控件,以处理与环境对象本身。

我认为我能做到这一点的唯一方法是将字典值作为依赖属性传递,以便每个用户控件都可以做它的事情:),当然这可能是一种糟糕的方法。

感谢任何帮助

问候沃尔夫

EDIT* 将 UserControl 更改为 EnvironmentStateView

EDIT* 在修复上一个错误之后,我注意到出现了更多的绑定错误信息

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 也许是,但我不知道我应该如何或是否应该大声笑。

【问题讨论】:

    标签: c# wpf binding dependency-properties


    【解决方案1】:
    public static readonly DependencyProperty EnvironmentObjectProperty =
        DependencyProperty.Register(
            "EnvironmentObject",
            typeof(Framework.Environment),
            typeof(EnvironmentStateView),
            new PropertyMetadata(null));
    

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

    【讨论】:

    • Gaahh 你是对的,这会教我懒惰地复制一些代码:)。不幸的是,虽然:S.
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多