0

私の WPF アプリケーションではMainWindow、コントロールがあり、GraphControlXAML マークアップによってウィンドウ内にユーザー コントロールが配置されています。GraphControlが割り当てられており、(クラスから派生した)GraphControlViewModelアクセサリ コントロールが含まれています。そのタイプの実装の概要 (簡略化) は次のとおりです。GraphViewControl

GraphControl.xaml :

<UserControl
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:designer="clr-namespace:Designer"
  xmlns:GraphUI="clr-namespace:GraphUI;assembly=GraphUI"
  xmlns:GraphModel="clr-namespace:GraphModel;assembly=GraphModel">

  /* simplified document content */

  <UserControl.Resources>

    <ResourceDictionary>

      <DataTemplate DataType="{x:Type GraphModel:NodeViewModel}">

          /* data template definition here*/

      </DataTemplate>

    </ResourceDictionary>

  </UserControl.Resources>

  <UserControl.DataContext>
    <designer:GraphControlViewModel />
  </UserControl.DataContext>

  <DockPanel>

    <GraphUI:GraphView NodesSource="{Binding Graph.Nodes}" />

  </DockPanel>

</UserControl>

GraphControlViewModel.cs:

public class GraphControlViewModel : AbstractModelBase
{
    private GraphViewModel graph;

    public GraphViewModel Graph
    {
        get
        {
            return this.graph;
        }
        set
        {
            this.graph = value;

            this.OnPropertyChanged("Graph");
        }
    }

    // implementation here
}

GraphViewModel.cs:

public sealed class GraphViewModel
{
    private ImpObservableCollection<NodeViewModel> nodes;

    public ImpObservableCollection<NodeViewModel> Nodes
    {
        get
        {
            return this.nodes ?? ( this.nodes = new ImpObservableCollection<NodeViewModel>() );
        }
    }

    // implementation here
}

NodeViewModel.cs:

public sealed class NodeViewModel : AbstractModelBase
{
   // implementation here
}

GraphView.cs:

public partial class GraphView : Control
{
    // implementation of display details here

    public IEnumerable NodesSource
    {
        get
        {
            return (IEnumerable)this.GetValue(NodesSourceProperty);
        }
        set
        {
            this.SetValue(NodesSourceProperty, value);
        }
    }
}

アプリケーションは、発明されたように機能し、見た目も良く、DataTemplateView Model クラスに適切に適用されます。

ただし、現時点では、アクセシビリティのために、定義にx:key属性を追加する必要があります。DataTemplate

<DataTemplate x:Key="NodeViewModelKey" DataType="{x:Type GraphModel:NodeViewModel}">

    /* data template definition here*/

</DataTemplate>

そしてここで私の問題が発生します。MSDNのデータ テンプレートの概要ドキュメントに記載されているとおり:

If you assign this DataTemplate an x:Key value, you are overriding the implicit x:Key and the DataTemplate would not be applied automatically.

実際、x:Key属性を追加した後DataTemplate、View Model クラスには適用されません。

私の場合、プログラムで DataTemplate を適用するにはどうすればよいですか?

4

2 に答える 2

1

DataTemplateに依存関係プロパティを追加してから、次のGraphViewように使用しようとします。

<GraphUI:GraphView NodesSource="{Binding Graph.Nodes}" 
                   DataTemplate={StaticResource NodeViewModelKey}/>
于 2013-09-18T15:13:35.393 に答える
1

好きな名前を付ける場合GraphView

<GraphUI:GraphView x:Name="myGraph" NodesSource="{Binding Graph.Nodes}" />

ユーザーコントロールのコードビハインドで、次のことができます。

      myGraph.Resources.Add(
      new DataTemplateKey(typeof(NodeViewModel)), 
      Resources["NodeViewModelKey"]);
于 2013-09-18T15:22:18.960 に答える