【问题标题】:WPF : How to access observablecollection in a nested usercontrol?WPF:如何访问嵌套用户控件中的 observablecollection?
【发布时间】:2016-02-19 06:03:45
【问题描述】:

在我的应用程序中,我使用了两个 usercontrol:UserControl1 是主要的,在其中,我使用了 UserControl2 六次。

UserControl2 有几个组合框,我想从最终应用程序中动态填充它们。首先,我尝试将数据绑定到其中一个。

UserControl2 中的组合框如下所示:

 <ComboBox x:Name="VidTransform" Template="{DynamicResource BaseComboBoxStyle}" ItemContainerStyle="{DynamicResource BaseComboBoxItemStyle}" Grid.Row="1" ItemsSource="{Binding Path=DataContext.VidTransformsNames,ElementName=Ch_Parameters, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding Path=SelectedTransform,ElementName=Ch_Parameters, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

目前我只能使用这个 ObservableCollection 手动填充它(所有字符串都正确显示):

    private ObservableCollection<string> _VidTransformsNames = new ObservableCollection<string>(new[] { "test0", "test1", "test2", "test3", "test4", "test5" });
    public ObservableCollection<string> VidTransformsNames
    {
        get { return _VidTransformsNames; }
        set { _VidTransformsNames = value; }
    }

在 UserControl1(其中包含 UserControl2)中,我尝试创建另一个 ObservableCollection 并在运行时在我的最终应用程序中动态填充它。

这里是:

    private ObservableCollection<string> _VideoTransformsNames = new ObservableCollection<string>(new[] { "Test0", "Test1", "Test2", "Test3", "Test4", "Test5" });
    public ObservableCollection<string> VideoTransformsNames
    {
        get { return _VideoTransformsNames; }
        set { _VideoTransformsNames = value; }
    }

然后绑定:

<local:UserControl1 VidTransformsNames="{Binding Path=VideoTransformsNames, ElementName=cmix, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

我是初学者,但在这里我肯定错了,因为我得到了这个错误:

无法在“UserControl1”类型的“VidTransformsNames”属性上设置“绑定”。 “绑定”只能在 DependencyObject 的 DependencyProperty 上设置。

如果 UserControl2 嵌套在 UserControl1 中,我如何在运行时访问和填充 Observablecollection?

【问题讨论】:

    标签: c# wpf user-controls observablecollection


    【解决方案1】:

    这是因为您的属性需要正确声明为DependencyProperty

    依赖属性和 WPF 属性系统扩展属性 通过提供支持属性的类型来实现功能,作为 支持标准模式的替代实现 具有私有字段的属性。这种类型的名称是 依赖属性。定义 WPF 的另一个重要类型 属性系统是 DependencyObject。 DependencyObject 定义了基础 可以注册并拥有依赖属性的类。

    请关注此https://msdn.microsoft.com/library/ms753358(v=vs.100).aspx,或依赖属性概述https://msdn.microsoft.com/pl-pl/library/ms752914(v=vs.100).aspx

    摘自以上文章的示例:

    public static readonly DependencyProperty IsSpinningProperty = 
        DependencyProperty.Register(
        "IsSpinning", typeof(Boolean),
    
    
    ...
    
    
        );
    public bool IsSpinning
    {
        get { return (bool)GetValue(IsSpinningProperty); }
        set { SetValue(IsSpinningProperty, value); }
    }
    

    让我们尝试将其设置为您的代码:

    public static readonly DependencyProperty VideoTransformsNamesProperty = 
    DependencyProperty.Register("VideoTransformsNames", typeof(ObservableCollection<string>), typeof(UserControl1));
    
    public string VideoTransformsNames
    {
        get
        {
            return this.GetValue(VideoTransformsNamesProperty) as ObservableCollection<string>;
        }
        set
        {
            this.SetValue(VideoTransformsNamesProperty, value);
        }
    }
    

    【讨论】:

    • 你到底在说什么属性?我应该使用 DependencyProperty 而不是 ObservableCollection 吗?
    • 您必须创建public static readonly DependecyProperty YourName,然后正确注册。在注册部分,您必须在您的案例中添加typeof(ObservableCollection&lt;T&gt;)
    • 确实,这正在工作。我不知道 observablecollection 可能是一种依赖属性。感谢您的帮助。
    • 很高兴能帮上忙。请将我的答案标记为正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多