0

私がやっていることは奇妙だと知っていますが、これを機能させたいと思っています。私はどこか間違っていると感じています。

次のように、リソースで定義された DataTemplate があります。

  <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ParameterEditorResourceDictionary.xaml"></ResourceDictionary>
            <ResourceDictionary>

                <DataTemplate x:Key="ParameterDefault">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="("></TextBlock>
                        <ItemsControl ItemsSource="{//I need to set from code}">
                            //some code here
                        </ItemsControl>
                        <TextBlock Text=")"></TextBlock>
                    </StackPanel>
                </DataTemplate>

          </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>       
</UserControl.Resources>

ロードされたイベントを持つ xaml で定義された DataGrid があります。

 <cc:PEDataGrid AutoGenerateColumns="False"
               Loaded="CommonPEGrid_Loaded">        
</cc:PEDataGrid>

私のイベント ハンドラー コードでは、DataTemplate で定義された ItemsControl の ItemsSource を設定したいと考えています。私のコードビハインドは次のようになります:

private void CommonPEGrid_Loaded(object sender, RoutedEventArgs e)
    {
        int i = 0;
        DataGrid dg = sender as DataGrid;

        DataGridTemplateColumn column = null;

        //ParametersAllLoops is a ObservableCollection

        foreach (ParameterLoop obj in ParametersAllLoops)
        {
            column = new DataGridTemplateColumn();
            column.Header = "Loop ( " + i.ToString() + " )";

            DataTemplate dt = null;

            //Here I want to write code
            //I want to access the DataTemplate defined in resources 
            //and set the ItemsSource of ItemsControl to something like this
            // xxx.ItemsSource = obj; and then assign the DataTemplate to 
            //the CellTemplate of column.
            //**Note :: ParameterLoop object has the IList Parameters**


            column.CellTemplate = dt;

            dg.Columns.Add(column);
            i++;            
        }
}
4

1 に答える 1

0

メソッド FindResource() を使用してリソースを検索し、それを DataTemplate にキャストできますが、ItemSource を割り当てるには、文字列操作が必要になります。

データグリッドに動的な列が必要なようです。コード ビハインドでデータ テンプレートを生成して、そこでバインディング パスとソース名を解決し、それをセル テンプレートまたはセル編集テンプレートとしてアタッチできるようにすることをお勧めします。

于 2010-10-12T13:57:57.957 に答える