2013-11-01 51 views
0

所以,我有不同datatemplates一个listview这里看到的物体:使用多个树视图具有相同(但克隆)的ItemsSource

<ListView Panel.ZIndex="0" x:Name="FilterList" Margin="10,0" Grid.Row="2" 
    Grid.ColumnSpan="3" Background="White" ItemTemplateSelector="{StaticResource 
    ReportFilterTemplateSelector}" ItemsSource="{Binding reportParameters, 
    Mode=TwoWay}" ScrollViewer.CanContentScroll="False"> 

我的一个样本datatemplates的可以看到下面。一切都显得很棒。我的问题是,对于这个(和其他)datatemplates,我可以有多个同一个实例。在这种特定情况下,treeviewitemssource势必DataContext.OfficeListText填充的所有元素。

<DataTemplate x:Key="office"> 
     <Grid MinHeight="35" MaxHeight="250"> 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="100"/> 
       <ColumnDefinition/> 
      </Grid.ColumnDefinitions> 
      <TextBlock Text="{Binding rpName}" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" /> 
      <Expander HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="1" 
       Header="{Binding Path=DataContext.OfficeListText, RelativeSource= 
       {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
       VerticalAlignment="Top" ExpandDirection="Down"> 

       <TreeView Tag="{Binding rpParameter}" HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" ItemsSource="{Binding 
        Path=DataContext.OfficeList, RelativeSource={RelativeSource 
        FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}" 
        ItemTemplate="{StaticResource CheckBoxItemTemplate}" 
        ItemContainerStyle="{StaticResource TreeViewItemStyle}"/> 
      </Expander> 
     </Grid> 
    </DataTemplate> 

的主要问题,这是一个,例如,如果我选择一个办公室说,第一treeview,第二treeview显示了相同的。基本上我希望他们最初有相同的itemssource,但有不同的实例。由于它们是动态生成的,所以我陷入困境。任何帮助,将不胜感激。

我不确定其他代码是否有必要,因为我相信大部分代码都是不合适的,这取决于我需要做什么来完成这项工作,但如果您想要更多,我会很乐意提供。谢谢!

+0

你能否详细说明在“如果我选择一个办公室在说第一个树视图,第二个树视图显示相同”? – foosburger

+0

嗯,数据模板用于绑定数据...如果你想要不同的数据源,你应该绑定到不同的实例...我相信你应该能够在控件的资源中创建一个实例并绑定到这个实例上我并没有真正了解你的问题的全貌,所以你可能想改进它... – UIlrvnd

+0

@foosburger说两个“办公室”数据模型被显示,一个会发生什么影响另一个。我假设这是因为树视图本身绑定到相同的'DataContext.OfficeList'实例。 – jmgardn2

回答

0

目前您的TreeView控件绑定到OfficeList的单个实例属于你的用户控件的DataContext的。这意味着每个TreeView都指向相同的列表。如果我正确理解你的问题,你真正想要的是为每个TreeView拥有一个不同的OfficeList实例。

我不建议的DataTemplate应用于reportParameter每次实例化一个新OfficeList。你可以用一个ValueConverter来做到这一点,但它会非常黑客。

的清洁解决方案是有一个包含数据的reportParameter一类,加上OfficeList的一个实例,然后绑定到类,而不是绑定到该用户控件的实例。根据reportParameters的结构(我假设reportParameters是ReportParameter类型的对象列表),有两种方法可能需要执行此操作:

1)如果ReportParameter是ViewModel,则可以只需将一个OfficeList属性添加到R​​eportParameter类中,并在实例化每个ReportParameter时对其进行初始化。

然后你TreeView的是这样的:

<TreeView Tag="{Binding rpParameter}" HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch" ItemsSource="{Binding Path=OfficeList, Mode=TwoWay}" 
    ItemTemplate="{StaticResource CheckBoxItemTemplate}" 
    ItemContainerStyle="{StaticResource TreeViewItemStyle}"/> 

2)如果ReportParameter不是一个视图模型(因此它不会是适当补充的OfficeList参数),然后创建一个名为类似的新类包含2个属性的ReportParameterViewModel:ReportParameter和OfficeList。不是将您的ListView绑定到ReportParameter列表,而是绑定到ReportParameterViewModel列表。

那么你的ListView会是这个样子:

<ListView Panel.ZIndex="0" x:Name="FilterList" Margin="10,0" Grid.Row="2" 
    Grid.ColumnSpan="3" Background="White" ItemTemplateSelector="{StaticResource 
    ReportFilterTemplateSelector}" ItemsSource="{Binding reportParameterViewModels, 
    Mode=TwoWay}" ScrollViewer.CanContentScroll="False"> 

你的TextBlock看起来就像这样:

<TextBlock Text="{Binding ReportParameter.rpName}" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0" /> 

你的TreeView看起来就像这样:

<TreeView Tag="{Binding rpParameter}" HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch" ItemsSource="{Binding Path=OfficeList, Mode=TwoWay}" 
    ItemTemplate="{StaticResource CheckBoxItemTemplate}" 
    ItemContainerStyle="{StaticResource TreeViewItemStyle}"/> 
相关问题