-->

是否有可能通过多种资源进入一个DataTemplate?(Is it possible to pas

2019-09-16 15:24发布

我想重用在ListView多列一个DataTemplate。 给出的两个XmlDataProvider通过使用在第一选择的项目I选择从第二值。 如果我指定的额外资源这工作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仅仅是我想要做的事的占位符。 研究didnt透露任何可能将其他参数传递到一个DataTemplate -但我不能相信这样的东西有用没有隐藏的地方。

所以,我如何通过一个额外的资源到DataTemplate旁边DataContext

Answer 1:

大量的调试和讨论后,我找到了解决上述问题。 传递额外的数据到一个模板中的一个可以附加属性层次结构中的父元素。 不幸的是,我们有机会的事情- 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 ,需要只是改变它的工作对这个资源是一个非常好的解决方案。



文章来源: Is it possible to pass multiple resources into a DataTemplate?