简体   繁体   English

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

[英]CollectionViewSource Binding not updating when removing or adding item

I'm having trouble with my combobox updating when binding to a list. 绑定到列表时我的组合框更新有问题。 All other controls properly update when I make changes. 我进行更改时,所有其他控件都会正确更新。 I'm attempting to remove an item from the drop down list from a button. 我正在尝试从按钮中删除下拉列表中的项目。

<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; }
    }

The issue was with the way I was manipulating the statusList. 问题在于我操纵statusList的方式。 I had statusList = another list, which didn't trigger the property changed. 我有statusList =另一个列表,它没有触发属性更改。 Worked around it with a for loop like below. 使用下面的for循环来解决它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF:Datagrid在绑定集合中添加或删除项目后正在更新标头 - WPF: Datagrid is updating header after adding or removing item at binding collection 通过Item绑定CollectionViewSource不会在添加项时触发Converter - Binding CollectionViewSource through Converter not triggering Converter when Item is added Windows 8 CollectionViewSource ObservableCollection绑定未更新 - Windows 8 CollectionViewSource ObservableCollection Binding Not Updating 添加/删除项目后更新Listview - Updating Listview after adding/removing item 使用linq在列表中添加/更新/删除项目 - adding/updating/removing item in a list with linq 与CollectionViewSource绑定 - Binding with CollectionViewSource 为什么在单击按钮时collectionviewsource不更新datagrid? - Why collectionviewsource not updating the datagrid when clicking a button? 优化添加/删除/更新集合中新项目的方式 - Optimize way of adding/removing/updating a new item in a collection 使用Parallel创建绑定项时,CollectionViewSource.SortDescriptions不起作用 - CollectionViewSource.SortDescriptions not work when binding items are created using Parallel Windows 8.1-绑定CollectionViewSource时如何获取元素的索引 - Windows 8.1 - How to get the index of elements when binding a CollectionViewSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM