【问题标题】:List View Selected Item Binding in wpf mvvmwpf mvvm中的列表视图选定项绑定
【发布时间】:2016-05-13 09:16:28
【问题描述】:

我在 wpf mvvm 模式中使用 ListView,其 SelectedItem 绑定已完成到 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");

            }
        }




    }

请建议我应该做什么更正,当我检查复选框时它应该直接命中绑定。

【问题讨论】:

标签: wpf listview mvvm


【解决方案1】:

您应该将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 放入 Single

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

并在您的视图模型中执行此操作

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

【讨论】:

  • 如果我选择不同的项目而不是单击 CheckBox,SelectedItem.IsSelected 属性值会怎样?
猜你喜欢
  • 2015-03-18
  • 2016-10-21
  • 2013-11-15
  • 2021-04-16
  • 2012-12-15
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多