3

我在项目中有 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(作为 SelectedItem)还是设置 CustomerID(作为 SearchText)?

4

1 回答 1

2

您应该执行以下操作:

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

    public CustomerViewModel SearchItem
    {
        get { return _searchItem; }
        set
        {
            if(value == _searchItem)
                return;
            _searchItem = value;
            RaisePropertyChanged("SearchItem");
        }
    }
    
于 2012-08-16T06:41:06.353 回答