【问题标题】:How to set SelectedItem in listview control?如何在列表视图控件中设置 SelectedItem?
【发布时间】:2012-08-16 06:30:16
【问题描述】:

我在项目中有 UserControl(AllCustomerView),它的 Viewmodel 作为 ALlCustomerViewModel 包含一个作为 SearchText 的属性。 SearchText 是 UserControl 中列表视图内 TextBox 的选定值。 SearchItem 设置为 SearchText 的 customerViewmdel。 但是在 listview 中, SearchItem 没有设置为选中

在 AllCustomerView.xaml 中

<TextBlock>
<TextBox 
        Width="150" 
        Text="{Binding Path=SearchText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</TextBox>
<Button 
         Command="{Binding Path=SearchCommand}"
         Content=" Search ">
</Button>
</TextBlock>

<ListView
            SelectedValue="{Binding Path=SearchText}"
            ItemsSource="{Binding Path=AllCustomers}"
            FontFamily="Tahoma"
            FontSize="14"
            ItemContainerStyle="{StaticResource CustomerItemStyle}" 
            IsSynchronizedWithCurrentItem="True">
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button 
                                    Command="{Binding ElementName=Root, Path=DataContext.DeleteCommand}"
                                    Content="x"
                                    FontFamily="Tahoma"
                                    FontSize="10"
                                    />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button 
                                     FontFamily="Tahoma"
                                    FontSize="10"
                                    Content="Edit"
                                    Command="{Binding Path=DataContext.Editcommand,ElementName=Root}"
                                    />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn
                        Header="CustomerID"
                        Width="130"
                        DisplayMemberBinding="{Binding Path=CustomerID}"/>
                    <GridViewColumn
                        Header="Contact Name"
                        Width="130"
                        DisplayMemberBinding="{Binding Path=ContactName}"/>
                    <GridViewColumn
                        Header="Company Name"
                        Width="130"
                        DisplayMemberBinding="{Binding Path=CompanyName}"/>
                    <GridViewColumn
                        Width="130"
                        Header="Contact Name"
                        DisplayMemberBinding="{Binding Path=ContactTitle}"
                        />
                </GridView>
            </ListView.View>
        </ListView>

及其视图模型(AllcustomerViewModel.cs)

public ICommand SearchCommand
        {
            get
            {
                if (_search == null)
                    _search = new Relaycommand(SearchCustomer);
                return _search;
            }
        }

 public void SearchCustomer(object o)
 {
            System.Windows.Forms.MessageBox.Show(SearchItem.ToString());

}

 public string searchText;
 public string SearchText
 {
     get { return searchText; }
     set 
     {
       searchText = value;
       var customerExsits = AllCustomers.Where(q => q.CustomerID.Trim().Equals(searchText));
        if (customerExsits.Any())
                {
                    SearchItem = customerExsits.Single();
                }

            }
        }

public CustomerViewModel SearchItem
{
            get;
            set;
}

ListView的SelectedValue应该设置什么,是设置Customerviewmodel(as SelectedItem)还是设置CustomerID(as SearchText)?

【问题讨论】:

    标签: wpf xaml data-binding mvvm


    【解决方案1】:

    您应该执行以下操作:

    1. 在您的ListView 中使用此绑定:SelectedItem="{Binding Path=SearchItem }"。不要使用SelectedValue
    2. 在您的 ViewModel 中实现 INotifyPropertyChanged 并在 SearchItem 属性的设置器中引发 PropertyChanged 事件。显然,您需要将此属性从自动属性更改为带有支持字段的经典属性:

      public CustomerViewModel SearchItem
      {
          get { return _searchItem; }
          set
          {
              if(value == _searchItem)
                  return;
              _searchItem = value;
              RaisePropertyChanged("SearchItem");
          }
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多