是否可以将多个资源传递到DataTemplate?

时间:2012-04-18 13:34:09

标签: wpf datatemplate multibinding

我想在ListView中为多个列重用DataTemplate。给定两个XmlDataProvider我通过使用第一个中的选定项来从第二个中选择值。 如果我在DataTemplate中指定其他资源,则此方法有效。但这迫使我复制DataTemplate的代码并只交换addtional资源。 我想做的是:

<Window x:Class="LayoutTests.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:LayoutTests"
        Title="Window2" Height="300" Width="300">
  <Window.Resources>
    <XmlDataProvider x:Key="XmlDataA" IsInitialLoadEnabled="True">
      <x:XData>
        <Items xmlns="">
          <Item id="1" text="A:1"/>
          <Item id="2" text="A:2"/>
          <Item id="3" text="A:3"/>
        </Items>
      </x:XData>
    </XmlDataProvider>
    <XmlDataProvider x:Key="XmlDataB" IsInitialLoadEnabled="True">
      <x:XData>
        <Items xmlns="">
          <Item id="1" text="B:1"/>
          <Item id="2" text="B:2"/>
          <Item id="3" text="B:3"/>
        </Items>
      </x:XData>
    </XmlDataProvider>
    <local:MultiXmlConverter x:Key="MultiXmlConverter"/>
    <local:DatabindingDebugConverter x:Key="DatabindingDebugConverter"/>
    <DataTemplate x:Key="Template" >
      <TextBlock Text="{Binding Converter={StaticResource MultiXmlConverter}}"/>
    </DataTemplate>
  </Window.Resources>
  <Grid>
    <ListView ItemsSource="{Binding Source={StaticResource XmlDataA}, XPath='/Items/Item'}" Background="Transparent">
      <ListView.View>
        <GridView>
          <GridViewColumn CellTemplate="{StaticResource Template}">
            <GridViewColumn.DisplayMemberBinding>
              <MultiBinding>
                <Binding Path="/"/>
                <Binding Source="{StaticResource XmlDataB}"/>
              </MultiBinding>
            </GridViewColumn.DisplayMemberBinding>
          </GridViewColumn>
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</Window>

为了完整性(和参考),这里有一个可能的转换器:

  public class MultiXmlConverter : IMultiValueConverter
  {
    public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      var element = value[0] as XmlElement;
      var dataProvider = value[1] as XmlDataProvider;
      XmlNodeList nodes = dataProvider.Document.SelectNodes("/Items/Item/[@id='" + element.Attributes["id"].Value.ToString() + "']");
      return nodes[0].Attributes["Text"].Value.ToString();
    }
    public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new Exception("The method or operation is not implemented.");
    }
  }

请注意,上面的XAML代码不起作用并产生以下错误:“无法设置MultiBinding,因为必须指定MultiValueConverter。”。 MultiBinding仅仅是我想要做的事情的占位符。研究没有揭示任何将其他参数传递给DataTemplate的可能性 - 但我不相信某些有用的东西不会隐藏在某处。

那么如何将其他资源传递到DataTemplate 旁边的DataContext

1 个答案:

答案 0 :(得分:0)

经过大量的调试和讨论后,我找到了解决上述问题的方法。 要将其他数据传递给模板,可以将属性附加到层次结构中的父元素。不幸的是,我们有权访问 - GridViewColumn没有出现在可视化树中。为了能够指定正确的资源,我们必须将内容包装起来。我修改上面的例子是完整的,所以这有点长:

<Window x:Class="LayoutTests.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:LayoutTests"
        Title="Window2" Height="300" Width="300">
  <Window.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
          <XmlDataProvider x:Key="XmlDataA" IsInitialLoadEnabled="True">
            <x:XData>
              <Items xmlns="">
                <Item id="1" text="A:1"/>
                <Item id="2" text="A:2"/>
                <Item id="3" text="A:3"/>
              </Items>
            </x:XData>
          </XmlDataProvider>
        </ResourceDictionary>
        <ResourceDictionary>
          <XmlDataProvider x:Key="XmlDataB" IsInitialLoadEnabled="True">
            <x:XData>
              <Items xmlns="">
                <Item id="1" text="B:1"/>
                <Item id="2" text="B:2"/>
                <Item id="3" text="B:3"/>
              </Items>
            </x:XData>
          </XmlDataProvider>
        </ResourceDictionary>
        <ResourceDictionary>
          <local:MultiXmlConverter x:Key="MultiXmlConverter"/>
          <local:DatabindingDebugConverter x:Key="DatabindingDebugConverter"/>
          <DataTemplate x:Key="Template" >
            <TextBlock>
              <TextBlock.Text>
                <MultiBinding Converter="{StaticResource MultiXmlConverter}">
                  <Binding/>
                  <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(local:Window2.AttachedXmlDataProvider)"/>
                </MultiBinding>
              </TextBlock.Text>
            </TextBlock>
          </DataTemplate>
          <DataTemplate x:Key="TemplateA">
            <ContentPresenter ContentTemplate="{StaticResource Template}" local:Window2.AttachedXmlDataProvider="{StaticResource XmlDataA}"/>
          </DataTemplate>
          <DataTemplate x:Key="TemplateB">
            <ContentPresenter ContentTemplate="{StaticResource Template}" local:Window2.AttachedXmlDataProvider="{StaticResource XmlDataB}"/>
          </DataTemplate>
        </ResourceDictionary>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Window.Resources>
  <Grid>
    <ListView ItemsSource="{Binding Source={StaticResource XmlDataA}, XPath='/Items/Item'}" Background="Transparent">
      <ListView.View>
        <GridView>
          <GridViewColumn CellTemplate="{StaticResource TemplateA}"/>
          <GridViewColumn CellTemplate="{StaticResource TemplateB}"/>
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</Window>

来自.cs文件的内容:

  public partial class Window2 : Window
  {
    public Window2()
    {
      InitializeComponent();
    }

    public static readonly DependencyProperty AttachedXmlDataProviderProperty =
    DependencyProperty.RegisterAttached("AttachedXmlDataProvider",
                                         typeof(XmlDataProvider),
                                         typeof(Window2),
                                         new FrameworkPropertyMetadata((XmlDataProvider)null, FrameworkPropertyMetadataOptions.AffectsRender));
    public static void SetAttachedXmlDataProvider(DependencyObject depObj, XmlDataProvider value)
    {
      depObj.SetValue(AttachedXmlDataProviderProperty, value);
    }
    public static XmlDataProvider GetAttachedXmlDataProvider(DependencyObject depObj)
    {
      return (XmlDataProvider)depObj.GetValue(AttachedXmlDataProviderProperty);
    }
  }

  public class MultiXmlConverter : IMultiValueConverter
  {
    public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      var element = value[0] as XmlElement;
      var dataProvider = value[1] as XmlDataProvider;
      string id = element.Attributes["id"].Value.ToString();
      if( dataProvider.Document == null )
        return null;
      XmlNodeList nodes = dataProvider.Document.SelectNodes("/Items/Item[@id='" + id + "']");
      string result = nodes[0].Attributes["text"].Value;
      return result;
    }
    public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new Exception("The method or operation is not implemented.");
    }
  }

上面代码的优点在于我们在DataTemplate中集成了不同的资源,并且只需要少量代码即可交换资源。即写一个简单地包装真实模板的DataTemplate。 从上面的例子中可能并不明显,但是如果你有一个真正复杂的DataTemplate并且需要改变它正在处理的资源,这是一个非常好的解决方案。