WPF:如何访问嵌套用户控件中的 observablecollection?
WPF : How to access observablecollection in a nested 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}"/>
我是初学者,但我肯定错了,因为我得到了这个错误:
A 'Binding' cannot be set on the 'VidTransformsNames' property of type 'UserControl1'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
如果 UserControl2 的 observablecollection 嵌套在 UserControl1 中,我如何在运行时访问和填充它?
这是因为您的 属性 需要正确声明为 DependencyProperty
Dependency properties and the WPF property system extend property
functionality by providing a type that backs a property, as an
alternative implementation to the standard pattern of backing the
property with a private field. The name of this type is
DependencyProperty. The other important type that defines the WPF
property system is DependencyObject. DependencyObject defines the base
class that can register and own a dependency property.
请关注这个https://msdn.microsoft.com/library/ms753358(v=vs.100).aspx, or Dependency Property Overview 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);
}
}
在我的应用程序中,我使用了两个用户控件: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}"/>
我是初学者,但我肯定错了,因为我得到了这个错误:
A 'Binding' cannot be set on the 'VidTransformsNames' property of type 'UserControl1'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
如果 UserControl2 的 observablecollection 嵌套在 UserControl1 中,我如何在运行时访问和填充它?
这是因为您的 属性 需要正确声明为 DependencyProperty
Dependency properties and the WPF property system extend property functionality by providing a type that backs a property, as an alternative implementation to the standard pattern of backing the property with a private field. The name of this type is DependencyProperty. The other important type that defines the WPF property system is DependencyObject. DependencyObject defines the base class that can register and own a dependency property.
请关注这个https://msdn.microsoft.com/library/ms753358(v=vs.100).aspx, or Dependency Property Overview 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);
}
}