gpt4 book ai didi

wpf - 如何在 ListView 控件中设置SelectedItem?

转载 作者:行者123 更新时间:2023-12-01 22:06:36 24 4
gpt4 key购买 nike

我在项目中有 UserControl(AllCustomerView),它的 Viewmodel 作为 ALLCustomerViewModel 包含一个属性作为 SearchText。SearchText 是 UserControl 中 ListView 内 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>

及其 View 模型(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)?

最佳答案

您应该执行以下操作:

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

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

关于wpf - 如何在 ListView 控件中设置SelectedItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11981793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com