列表在wpf mvvm

时间:2016-05-13 09:16:29

标签: wpf listview mvvm

我在wpf mvvm模式中使用ListView,其SelectItem绑定是对ViewModel完成的。我面临的问题是,只要选中复选框,SelectedItem绑定就不会立即生效。只有当我再次单击复选框外的某个位置及其各自的内容时,它才有效。

我的ListView是这样的:

<Grid>
            <Grid.Resources>
                <DataTemplate x:Key="checkboxHeaderTemplate">
                    <CheckBox IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=UserControl },Mode=TwoWay}">
                    </CheckBox>
                </DataTemplate>

                <DataTemplate x:Key="CheckBoxCell">
                    <!--<CheckBox Checked="CheckBox_Checked" />-->
                    <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                    </CheckBox>
                </DataTemplate>

                <DataTemplate x:Key="TextCell">
                    <TextBlock  Text="Usecasename">
                    </TextBlock>
                </DataTemplate>

                <DataTemplate x:Key="ButtonCell">
                    <Button  Content="{Binding Path=UsecaseName, Mode=TwoWay}" >
                    </Button>
                </DataTemplate>
            </Grid.Resources>

            <ListView SelectedItem="{Binding SelectedSection}" ItemsSource="{Binding Path=UsecaseListItems}" >
                <ListView.View>
                    <GridView>
                        <GridView.Columns>

                            <GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}"
                                        CellTemplate="{StaticResource CheckBoxCell}" Width="auto">
                            </GridViewColumn>

                            <GridViewColumn HeaderTemplate="{StaticResource TextCell}"
                                        CellTemplate="{StaticResource ButtonCell}" Width="auto">
                            </GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>

我使用它绑定List View的Selected itm的HomeViewModel是这样的:

private UseCase _selectedSection;
        public UseCase SelectedSection
        {
            get { return _selectedSection; }
            set
            {
                _selectedSection = value;
                if (this.SelectedSection.UsecaseName == ("CCS01") && (this.SelectedSection.IsSelected == true))
                {
                    this.ContentWindow = new CCS01();
                }
               else if (this.SelectedSection.UsecaseName == ("CCS02") && (this.SelectedSection.IsSelected == true))
                {
                    this.ContentWindow = new CCS02();
                }
                else if (this.SelectedSection.UsecaseName == ("ECS52") && (this.SelectedSection.IsSelected == true))
                {
                    this.ContentWindow = new ECS52();
                }
                else
                    this.ContentWindow = new Default();
                OnPropertyChanged("SelectedSection");
            }
        }

和UseCase类是这样的:

public class UseCase: BaseNotifyPropertyChanged
    {
        public string UsecaseName { get; set; }



        private bool _IsSelected;
        public bool IsSelected
        {
            get { return _IsSelected; }
            set
            {
                _IsSelected = value;
                OnPropertyChanged("IsSelected");

            }
        }




    }

请建议我应该做哪些更正,以便在检查复选框时直接点击绑定。

1 个答案:

答案 0 :(得分:0)

您应该将CheckBoxCell的复选框绑定更改为以下内容:

<DataTemplate x:Key="CheckBoxCell">
                <!--<CheckBox Checked="CheckBox_Checked" />-->
                <CheckBox IsChecked="{Binding Path=IsSelected, 
                          RelativeSource={RelativeSource FindAncestor, 
                  AncestorType={x:Type ListViewItem}}, 
                          Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                </CheckBox>
            </DataTemplate>

对于这项工作,您需要将SelectionMode置于单

 <ListView SelectedItem="{Binding SelectedSection}" 
ItemsSource="{Binding Path=UsecaseListItems}" SelectionMode="Single">

在viewmodel中执行此操作

private UseCase _selectedSection;
        public UseCase SelectedSection
        {
            get { return _selectedSection; }
            set
            {
                DeSelectAll();
                _selectedSection = value;
                _selectedSection.IsSelected = true;
                OnPropertyChanged("SelectedSection");
            }
        }

        private void DeSelectAll()
        {
            foreach (var item in UsecaseListItems)
            {
                item.IsSelected = false;
            }
        }