如何顺利合并绑定到 SAME obj 的用户控件?
How to Merge user controls that Binds to SAME obj smoothly?
我正在尝试合并一些绑定到同一目标的用户控件。一开始,它看起来很简单,但我不知道如何将绑定目标传递给子控件(合并控件内的控件)?
我想做这个:
<Canvas>
<local:Teeth x:Name="sideR" Points="{Binding Points[0]}" IsClosedCurve="{Binding IsClosedCurve}"/>
<local:WrapTeeth Points="{Binding Points[0]}"/>
<ListBox ItemsSource="{Binding Points[0]}" ItemContainerStyle="{StaticResource PointListBoxItemStyle}">
<ListBox.Template>
<ControlTemplate>
<Canvas IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
</ListBox>
</Canvas>
进入
<local:MergeControl Points="{Binding Points[0]}"/>
您的 UserControl 应该有一个 Points
依赖项 属性,如下所示。从您的问题中不清楚您是否需要比 IEnumerable
更专业的集合类型。可能用 PointCollection
或更合适的东西替换它。
public partial class MergeControl : UserControl
{
public static readonly DependencyProperty PointsProperty = DependencyProperty.Register(
"Points", typeof(IEnumerable), typeof(MergeControl));
public IEnumerable Points
{
get { return (IEnumerable)GetValue(PointsProperty); }
set { SetValue(PointsProperty, value); }
}
public MergeControl()
{
InitializeComponent();
}
}
UserControl 的 XAML 中的元素将通过 RelativeSource
绑定绑定到此 属性。您可能需要为 Teeth
元素的 IsClosedCurve
绑定定义另一个 属性。
<UserControl ...>
<UserControl.Resources>
<Style x:Key="PointListBoxItemStyle" TargetType="ListBoxItem">
...
</Style>
</UserControl.Resources>
<Canvas>
<local:Teeth x:Name="sideR"
Points="{Binding Points, RelativeSource={RelativeSource AncestorType=UserControl}}"
IsClosedCurve="{Binding IsClosedCurve, ...}"/>
<local:WrapTeeth
Points="{Binding Points, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
<ListBox
ItemsSource="{Binding Points, RelativeSource={RelativeSource AncestorType=UserControl}}"
ItemContainerStyle="{StaticResource PointListBoxItemStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Canvas>
</UserControl>
另请注意,ItemsControls 有一个 ItemsPanel
属性 来设置用于包含其项目的 Panel 元素。
我正在尝试合并一些绑定到同一目标的用户控件。一开始,它看起来很简单,但我不知道如何将绑定目标传递给子控件(合并控件内的控件)?
我想做这个:
<Canvas>
<local:Teeth x:Name="sideR" Points="{Binding Points[0]}" IsClosedCurve="{Binding IsClosedCurve}"/>
<local:WrapTeeth Points="{Binding Points[0]}"/>
<ListBox ItemsSource="{Binding Points[0]}" ItemContainerStyle="{StaticResource PointListBoxItemStyle}">
<ListBox.Template>
<ControlTemplate>
<Canvas IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
</ListBox>
</Canvas>
进入
<local:MergeControl Points="{Binding Points[0]}"/>
您的 UserControl 应该有一个 Points
依赖项 属性,如下所示。从您的问题中不清楚您是否需要比 IEnumerable
更专业的集合类型。可能用 PointCollection
或更合适的东西替换它。
public partial class MergeControl : UserControl
{
public static readonly DependencyProperty PointsProperty = DependencyProperty.Register(
"Points", typeof(IEnumerable), typeof(MergeControl));
public IEnumerable Points
{
get { return (IEnumerable)GetValue(PointsProperty); }
set { SetValue(PointsProperty, value); }
}
public MergeControl()
{
InitializeComponent();
}
}
UserControl 的 XAML 中的元素将通过 RelativeSource
绑定绑定到此 属性。您可能需要为 Teeth
元素的 IsClosedCurve
绑定定义另一个 属性。
<UserControl ...>
<UserControl.Resources>
<Style x:Key="PointListBoxItemStyle" TargetType="ListBoxItem">
...
</Style>
</UserControl.Resources>
<Canvas>
<local:Teeth x:Name="sideR"
Points="{Binding Points, RelativeSource={RelativeSource AncestorType=UserControl}}"
IsClosedCurve="{Binding IsClosedCurve, ...}"/>
<local:WrapTeeth
Points="{Binding Points, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
<ListBox
ItemsSource="{Binding Points, RelativeSource={RelativeSource AncestorType=UserControl}}"
ItemContainerStyle="{StaticResource PointListBoxItemStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Canvas>
</UserControl>
另请注意,ItemsControls 有一个 ItemsPanel
属性 来设置用于包含其项目的 Panel 元素。