トップ回答者
DataGridComboBoxColumnでDynamicResourceを使った場合に望んだ挙動にならない

質問
-
お世話になります。
■概要
DataGridComboBoxColumnでDynamicResourceを使った場合に、リソースの中身が参照されず、
解決方法が判らなかったので質問させてください。
■やりたかったこと
DataGridのコンボボックスにリソースを指定して、コード側から動的にアイテムを追加するつもりでした。
ですのでDynamicsResourceを利用しています。
■サンプル
状況を再現するために単純な状態のサンプルをつくりました。
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="721"> <Window.Resources> <x:Array Type="system:String" x:Key="testItems" > <system:String>test-a</system:String> <system:String>test-b</system:String> </x:Array> </Window.Resources> <Grid> <ComboBox HorizontalAlignment="Left" Margin="42,78,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{DynamicResource testItems}"/> <DataGrid x:Name="dataList" Margin="182,30,0,0" AutoGenerateColumns="False" SelectionMode="Extended" SelectionUnit="Cell" FrozenColumnCount="3"> <DataGrid.Columns> <DataGridComboBoxColumn Header="test0" Width="120" ItemsSource="{StaticResource testItems}"/> <DataGridComboBoxColumn Header="test1" Width="120" ItemsSource="{DynamicResource testItems}"/> <DataGridTemplateColumn Header="test2" Width="120"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{DynamicResource testItems}"></ComboBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var data = new ObservableCollection<string[]>( Enumerable.Range(1, 3).Select(i => new string[] {"a", "b", "c"} ) ); this.dataList.ItemsSource = data; } } }
これを実行した場合、単独ComboBox, DataGridのColumnであるtest0, test2は望んだ挙動になります。
しかしtest1だけは、コンボボックスの中身が空になります。
これはどう解決するべきでしょうか?
以上よろしくお願い致します。
回答
-
DynamicResourceの場合、必要になる度にリソースを検索しに行きます。よって、コンボボックスを表示した際にもリソースを検索しに行きますが、コンボボックスはVisual Treeから外れているため、リソースを探せないのでしょう。今、検証できる状況に私がいませんので、Freezableを使った方法を紹介しておきます。
例
DataGridのカラムのVisibilityをバインドによって切り替えたい。
https://social.msdn.microsoft.com/Forums/ja-JP/8b2298ba-4910-47f5-854a-55c4bcde5725/datagridvisibility★良い回答には回答済みマークを付けよう! MVP - .NET http://d.hatena.ne.jp/trapemiya/
すべての返信
-
DynamicResourceの場合、必要になる度にリソースを検索しに行きます。よって、コンボボックスを表示した際にもリソースを検索しに行きますが、コンボボックスはVisual Treeから外れているため、リソースを探せないのでしょう。今、検証できる状況に私がいませんので、Freezableを使った方法を紹介しておきます。
例
DataGridのカラムのVisibilityをバインドによって切り替えたい。
https://social.msdn.microsoft.com/Forums/ja-JP/8b2298ba-4910-47f5-854a-55c4bcde5725/datagridvisibility★良い回答には回答済みマークを付けよう! MVP - .NET http://d.hatena.ne.jp/trapemiya/