WPF:需要一些绑定到UserControl的帮助吗

WPF:需要一些绑定到UserControl的帮助吗,wpf,user-controls,dependency-properties,Wpf,User Controls,Dependency Properties,我正在尝试构建一个包含2个列表框的UserControl。然后,我不想为使用UserControl的列表框设置itemsSources 据我所知,dependencProperties就是为了这个。然而,我没有成功地做到这一点。我相信这主要与初始化的时间有关。任何关于我做对了什么,我如何能做得更好的指针都是受欢迎的 这是我的用户控制,我在学习,所以我想我可以做得更好 <Grid> <Grid.ColumnDefinitions> <C

我正在尝试构建一个包含2个列表框的UserControl。然后,我不想为使用UserControl的列表框设置itemsSources

据我所知,
dependencProperties
就是为了这个。然而,我没有成功地做到这一点。我相信这主要与初始化的时间有关。任何关于我做对了什么,我如何能做得更好的指针都是受欢迎的

这是我的用户控制,我在学习,所以我想我可以做得更好

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition />
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>

    <ListBox Name="SET" ItemsSource="{Binding Path=SetCollection}" />

    <UniformGrid Rows="4" Grid.Column="1">

        <Grid />

        <Button Margin="10" Click="selectionBtnClick">--></Button>

        <Button Margin="10" Click="removeBtnClick">Remove</Button>

    </UniformGrid>

    <ListBox Name="SUBSET" ItemsSource="{Binding Path=SubsetCollection}" Grid.Column="2" />

</Grid>
还有我使用它的代码

public partial class SomeWindow : Window
{
    ViewModel vm = new ViewModel();

    public SomeWindow()
    {
        InitializeComponent();
        NameOfUserControl.SetCollection = vm.InputView;
        NameOfUserControl.SubsetCollection = vm.OutputView;
    }
}
在这里,SetCollection被设置为inputView,然后绑定到dependencProperty,断开原始绑定。知道我哪里出错了吗


附子问题-当我将要从一个集合移动到另一个集合时,我是否应该以某种方式确保集合保留同一类型的对象?我该怎么做呢?

首先,您应该将SomeWindow中的
DataContext
属性设置为vm。这将允许在SomeWindow.xaml中使用非常简单的绑定表达式

public partial class SomeWindow : Window
{
    ViewModel vm = new ViewModel();

    public SomeWindow()
    {
        InitializeComponent();
        this.DataContext = vm;
    }
}
在SomeWindow.xaml中:

<local:SubsetSelectionLists
    SetCollection="{Binding Path=InputView}"
    SubsetCollection ="{Binding Path=OutputView}" />
Wpf根据当前数据上下文绑定,除非设置了特定的源(例如ElementName)

将绑定与ElementName一起使用 可以在绑定表达式中引用用户控件

<UserControl x:Class="WpfApplication1.SubsetSelectionLists"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             x:Name="root">
<Grid>
...
    <ListBox Name="SET"
             ItemsSource="{Binding Path=SetCollection, ElementName=root}" />
...
    <ListBox Name="SUBSET" Grid.Column="2"
             ItemsSource="{Binding Path=SubsetCollection, ElementName=root}" />

</Grid>
</UserControl>
在用户控件中:

<Grid>
...
    <ListBox Name="SET"
             ItemsSource="{Binding Path=InputView}" />
...
    <ListBox Name="SUBSET" Grid.Column="2"
             ItemsSource="{Binding Path=OutputView}" />

</Grid>

...
...

现在不在我的电脑上,所以我会检查一下。我认为,如果我在userControl上创建DependencyProp,它将与其他类型(如margin或itemsSource)上的属性类似,但如果我说SetCollection=*binding*,它将在窗口的DataContext中查找SetCollection。我期望从绑定而不是从属性本身。
<UserControl x:Class="WpfApplication1.SubsetSelectionLists"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             x:Name="root">
<Grid>
...
    <ListBox Name="SET"
             ItemsSource="{Binding Path=SetCollection, ElementName=root}" />
...
    <ListBox Name="SUBSET" Grid.Column="2"
             ItemsSource="{Binding Path=SubsetCollection, ElementName=root}" />

</Grid>
</UserControl>
public partial class SomeWindow : Window
{
    ViewModel vm = new ViewModel();
    //with properties InputView and OutputView

    public SomeWindow()
    {
        InitializeComponent();
        NameOfUserControl.DataContext = vm;
    }
}
<Grid>
...
    <ListBox Name="SET"
             ItemsSource="{Binding Path=InputView}" />
...
    <ListBox Name="SUBSET" Grid.Column="2"
             ItemsSource="{Binding Path=OutputView}" />

</Grid>