我意識到我自己的日曆。 我做了一個Generic.xaml(ResourceDictionnary),其中包含我的新控件。我有一個Calendar.class誰實現:控制。ObservableCollection + ResourceDictionnary + Listbox PropertyChanged不起作用
在我的Calendar類中,我有一個ObservableCollection<Day> _days
。我把DataContext = this;
一天包含我的ObservableCollection<MyObject> ListeTache
。 而Day.Class執行INotifyPropertyChanged,並有我的事件:
public event PropertyChangedEventHandler PropertyChanged;
但是,當我更新我的列表框,我必須手動刷新我的日曆看到任何變化。 我錯過了什麼嗎?
謝謝你的幫助。
我ObservableCollection<MyObject>
:
public ObservableCollection<Tache> ListeTache
{
get { return this._listeTache; }
set
{
_listeTache = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ListeTache"));
}
}
}
我Generic.xaml是這樣的:
<Grid x:Name="LayoutTache">
<ListBox x:Name="ListeTaches" ItemsSource="{Binding ListeTache,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" FontSize="10" PreviewMouseDown="PreviewMouseDownClick_clear" MouseDoubleClick="doubleClic" IsSynchronizedWithCurrentItem="False">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding AffichageCalendrier}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Resources>
<ContextMenu x:Key="MonMenu">
<MenuItem Header="Supprimer" Click="MonMenuDel_Click" />
</ContextMenu>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="AntiqueWhite"></Setter>
<Setter Property="ContextMenu" Value="{StaticResource MonMenu}"/>
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen" />
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
索姆回覆之後:
我怎麼能這樣做?我要補充這樣的事在我Day.cs類:
_listeTache.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_listeTache_CollectionChanged);
void _listeTache_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
throw new NotImplementedException();
}
我永遠也趕不上的事件......
感謝所有
我認爲你的'Tache'類需要實現'INotifyPropertyChanged'以及你對'Tache'對象進行修改以及需要通知UI的東西。 – XAMlMAX
好的,我可以嘗試,但我只是添加或刪除ObservableCollection中的Tache,我不改變Tache本身。 –
trainor
對不起,沒有正確地看你的代碼,你是正確的你的'Tache'類不需要實現'INotifyPropertyChanged',但你有沒有嘗試刪除'ListChange'中的'PropertyChanged'的空檢查? – XAMlMAX