选择性地将数据绑定到集合

我想有选择地将集合中的数据绑定到不同的UI元素。例如,我有一个对象集合,其中每个对象都有一个描述该对象类型的属性。我目前正在使用DataTemplate对元素进行分类。但是当我将集合出价到两个不同的UI容器时,尽管DataTemplates可用,但是对象被添加到两个容器中。以下代码将使问题更清晰: 视图: 数据模板:
        <DataTemplate x:Key="ColumnDataTemplate">
        <Label DataContext="{Binding ColumnDiscriptor}" Foreground="White" Background="DarkOrange" BorderThickness="5">
            <TextBlock Text="{Binding ColumnName}">
            <!--<Run Text="{Binding TableName}"/><Run Text="."/><Run Text="{Binding ColumnName}"/>-->
            </TextBlock>
        </Label>
    </DataTemplate>
    <DataTemplate x:Key="ControllerDataTemplate">
        <Label Content="{Binding OperatorValue}" Foreground="White" Background="Crimson" BorderThickness="5">
        </Label>
    </DataTemplate>
    <DataTemplate x:Key="ValueDataTemplate">
        <TextBox Text="{Binding Path=ComparedValue, Mode= TwoWay}" Height="24" HorizontalAlignment="Left" Width="46" />
    </DataTemplate>

    <DataTemplate x:Key="JoinDataTemplate">
            <Grid>
            <!--<Run Text="{Binding TableName}"/><Run Text="."/><Run Text="{Binding ColumnName}"/>-->
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="80*"/>
                </Grid.RowDefinitions>

                <Label Grid.Row="0" Grid.Column="0" DataContext="{Binding ColumnDiscriptor}" Foreground="Black" Background="Brown" BorderThickness="5">
                    <TextBlock Text="{Binding ColumnName}">
                    </TextBlock>
                </Label>

            <Label Grid.Row="0" Grid.Column="1" DataContext="{Binding JoinWithColumnDescriptor}" Foreground="Black" Background="Beige" BorderThickness="5">
                <TextBlock Text="{Binding Path=ColumnName}">
                    </TextBlock>
                </Label>
            </Grid>
    </DataTemplate>

 <local:QueryObjectDataTemplateSelector x:Key="ModelSelector"
                                       ColumnTemplate="{StaticResource ColumnDataTemplate}"
                                       ControllerTemplate="{StaticResource ControllerDataTemplate}"
                                       ValueTemplate="{StaticResource ValueDataTemplate}"
                                       />
UI容器:
<ListBox  Height="146" HorizontalAlignment="Left" Margin="12,38,0,0" Name="visulaqueryPanel" VerticalAlignment="Top" Width="344" Background="#18EBEBE2" AllowDrop="True" Drop="Item_Drop" DragEnter="Item_DragEnter"  ItemsSource="{Binding ViewUIElements}" ItemsPanel="{StaticResource WrapPanelTemplate}" 
              ItemTemplateSelector="{StaticResource ModelSelector}" SelectedItem="{Binding Path=SelectedDesignerVisualQueryObject}"/>

<ListBox AllowDrop="True" Background="#1CFFFFFF" Margin="10,38,6,39" Name="grid1" Drop="Join_Item_Drop" DragEnter="Item_DragEnter" ItemsSource="{Binding ViewUIElements}" ItemTemplate="{StaticResource JoinDataTemplate}" SelectedItem="{Binding Path=SelectedDesignerVisualQueryObjectJoin}" />
根据我的问题,ViewUIElemetns是由不同类型的对象组成的集合。 提前致谢。     
已邀请:
只需根据Type定义不同的DataTemplates:
<DataTemplate DataType="{x:Type SomeType1}">
 ...
</DataTemplate> 

<DataTemplate DataType="{x:Type SomeType2}">
 ...
</DataTemplate> 
如果你必须按属性而不是按类型来做,那么你应该使用DataTemplateSelector: http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector     

要回复问题请先登录注册