I've noticed a behavior difference between static and dynamic resource on ComboBox.ItemsSource, when the ComboBox gets out the visual tree.
- in the static exemple the selected destination remains
- in the dynamic exemple, the underlying object gets a null value
Binding seems OK, because when the comboboxes gets in focus, and have their SelectedIndex changed, the change are properly notified to the other list - both objects implements INotifyProperty - and both List are ObservableCollections.
It's when the dynamic-bound combobox gets out of focus that the strange things happen
XAML
<Window ... xmlns:me = "clr-namespace:WpfComboBoxBug">
<Window.Resources>
<me:ShippingList x:Key="sl" />
<me:DestinationList x:Key="dl" />
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="21" />
<RowDefinition Height="421*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Custom:DataGrid Grid.Row="1"
ItemsSource="{StaticResource sl}" x:Name="dg" AutoGenerateColumns="False" Grid.RowSpan="2">
<Custom:DataGrid.Columns>
<Custom:DataGridTextColumn Header="Reference" Binding="{Binding Reference}" />
<Custom:DataGridTemplateColumn Header="Destination">
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Destination.Name}"></TextBlock>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
<Custom:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource dl}" SelectedItem="{Binding Destination,Mode=TwoWay}" DisplayMemberPath="Name"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellEditingTemplate>
</Custom:DataGridTemplateColumn>
</Custom:DataGrid.Columns>
</Custom:DataGrid>
<Custom:DataGrid Grid.Column="1" Grid.Row="1" ItemsSource="{StaticResource sl}" x:Name="dg2" AutoGenerateColumns="False" Grid.RowSpan="2">
<Custom:DataGrid.Columns>
<Custom:DataGridTextColumn Header="Reference" Binding="{Binding Reference}" />
<Custom:DataGridTemplateColumn Header="Destination">
<Custom:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Destination.Name}"></TextBlock>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellTemplate>
<Custom:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{DynamicResource dynamicdl}" SelectedItem="{Binding Destination,Mode=TwoWay}" DisplayMemberPath="Name"/>
</DataTemplate>
</Custom:DataGridTemplateColumn.CellEditingTemplate>
</Custom:DataGridTemplateColumn>
</Custom:DataGrid.Columns>
</Custom:DataGrid>
<TextBox Height="23" Name="textBox1" VerticalAlignment="Top" Text="Static" />
<TextBox Height="23" Name="textBox2" VerticalAlignment="Top" Text="Dynamic" Grid.Column="2" />
</Grid>
</Window>
CS
using System;
/* snip */
namespace WpfComboBoxBug
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
ShippingList sl;
this.InitializeComponent();
sl = this.Resources["sl"] as ShippingList;
ResourceDictionary rd = new ResourceDictionary();
rd.Add("dynamicdl", this.FindResource("dl"));
dg2.Resources = rd;
dg.ItemsSource = CollectionViewSource.GetDefaultView(sl);
dg2.ItemsSource = CollectionViewSource.GetDefaultView(sl);
}
}
}
full source code at : http://dl.free.fr/eI1VtkaB8 ( VS 2008 SP1, .NET 3.5 SP1 )
I expected the dynamic resource to behave like the static resource in this case, beacause I intialize it once in the beginning.
- Have I found a bug here ?
- If that' not the case, how would you explain the difference ?