CollectionViewSource绑定在删除或添加项目时不更新

时间:2015-08-19 00:18:56

标签: c# wpf binding

我在绑定到列表时遇到组合框更新问题。我进行更改时,所有其他控件都会正确更新。我试图从按钮中删除下拉列表中的项目。

<CollectionViewSource Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=statusList}" x:Key="statusList"/>

<ComboBox.ItemTemplate>
    <DataTemplate>
        <CheckBox Tag="{Binding ID}" Content="{Binding Name}" Foreground="White" Margin="10,0,0,0" Checked="Filter_Checked" Unchecked="Filter_Checked" IsChecked="{Binding ElementName=Filter21Days, Path=IsChecked, Mode=OneWay}"></CheckBox>
    </DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsSource>
<CompositeCollection>
    <ComboBoxItem Content="Filter" Visibility="Collapsed" IsEnabled="False"/>
    <ComboBoxItem>
        <CheckBox x:Name="ViewAllFilter" Content="View All" Foreground="White" Margin="10,0,0,0" Checked="Filter_Checked" Unchecked="Filter_Checked" IsChecked="False"></CheckBox>
    </ComboBoxItem>
    <ComboBoxItem>
        <CheckBox x:Name="Filter21Days" Content="Last 21 Days" Foreground="White" Margin="10,0,0,0" Checked="Filter_Checked" Unchecked="Filter_Checked" ></CheckBox>
    </ComboBoxItem>
    <ComboBoxItem>
        <CheckBox x:Name="FilterBydate" Content="By Date" Foreground="White" Margin="10,0,0,0" Checked="Filter_Checked" Unchecked="Filter_Checked" Visibility="Collapsed"></CheckBox>
    </ComboBoxItem>
    <ComboBoxItem>
        <Separator Background="White" Width="110" Height="1"></Separator>
    </ComboBoxItem>
    <CollectionContainer Collection="{Binding Source={StaticResource statusList}}"/>

</CompositeCollection>
</ComboBox.ItemsSource>

ObservableCollection<Status> _statusList = new ObservableCollection<Status>();
public ObservableCollection<Status> statusList
    {
        get { return _statusList; }
        set { _statusList = value; }
    }

1 个答案:

答案 0 :(得分:1)

问题在于我操纵statusList的方式。我有statusList =另一个列表,它没有触发属性更改。用下面的for循环来解决它。

statusList.Clear();
for (int i = 0; i < statusWindow.statusList.Count; i++)
{
    statusList.Add(statusWindow.statusList[i]);
}