0

ListViewの複数の列にDataTemplateを再利用したい。2つを考えるとXmlDataProvider、最初に選択した項目を使用して、2番目から値を選択します。これは、で追加のリソースを指定した場合に機能しますDataTemplate。しかし、これにより、DataTemplateのコードを複製し、追加のリソースを交換する必要があります。私がしたいのはこれです:

<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コードは機能せず、「MultiValueConverterを指定する必要があるため、MultiBindingを設定できません。」というエラーが発生することに注意してください。これMultiBindingは、私がやりたいことの単なるプレースホルダーです。調査では、追加のパラメーターを-に渡す可能性は明らかにされてDataTemplateいませんが、非常に便利なものがどこかに隠されていないことは信じられません。

では、どうすれば追加のリソースをDataTemplate隣のDataContextリソースに渡すことができますか?

4

1 に答える 1

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で、リソースを変更する必要がある場合は、これに取り組んでいるのは本当に素晴らしいソリューションです。

于 2012-04-19T15:25:07.013 に答える