Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过组合框中的选择更新datagrid中的源_C#_Wpf_Gridview_Mvvm_Combobox - Fatal编程技术网

C# 通过组合框中的选择更新datagrid中的源

C# 通过组合框中的选择更新datagrid中的源,c#,wpf,gridview,mvvm,combobox,C#,Wpf,Gridview,Mvvm,Combobox,我是WPF和MVVM设计模式的新手。因此,我尝试做的每件事似乎都需要很长时间 目前,我正在使用一个组合框来选择一个列表。我想用列表中的所有联系人填充Gridview,并在每次组合框选择更改时刷新该Gridview 视图的XAML如下所示。如何让Gridview监听组合框选择中的更改?我需要选择Combobox来设置它的属性,这样我就可以查询db来返回那些与列表相关联的记录,所以我的第一个想法是添加一个按钮来触发命令,但它从不触发。我在谷歌上读到过,它说Gridview应该监听更新的属性,但没有关

我是WPF和MVVM设计模式的新手。因此,我尝试做的每件事似乎都需要很长时间

目前,我正在使用一个组合框来选择一个列表。我想用列表中的所有联系人填充Gridview,并在每次组合框选择更改时刷新该Gridview

视图的XAML如下所示。如何让Gridview监听组合框选择中的更改?我需要选择Combobox来设置它的属性,这样我就可以查询db来返回那些与列表相关联的记录,所以我的第一个想法是添加一个按钮来触发命令,但它从不触发。我在谷歌上读到过,它说Gridview应该监听更新的属性,但没有关于如何做到这一点的示例

我在Gridview中尝试了这一点,但引发了一个异常,是的,我的ViewModel实现了InotifyPropertyChanged

PropertyChanged=“{Binding Path=SelectedListNameContactView}

谢谢你的帮助

<UserControl x:Name="manageLists"
         x:Class="Five9ContactManagement.ManageListsView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
         xmlns:my="clr-namespace:Five9ContactManagement.Controls;assembly=Five9ContactManagement.Controls"
         mc:Ignorable="d"
         d:DesignHeight="400"
         d:DesignWidth="400"
         xmlns:local="clr-namespace:Five9ContactManagement">


 <Grid>
   <Grid HorizontalAlignment="Center"
         VerticalAlignment="Top"
         Margin="20">
      <Grid.ColumnDefinitions>
       <ColumnDefinition />
       <ColumnDefinition />
       <ColumnDefinition />
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
      </Grid.RowDefinitions>

      <TextBlock  Grid.Column="0"
              Grid.Row="2"
              Text="Select List "
              VerticalAlignment="Center"
              Margin="5" />

      <ComboBox Grid.Column="1"
            Grid.Row="2"
            ItemsSource="{Binding GetLists, Mode=OneWay}"
            VerticalAlignment="Center"
            SelectedIndex="-1"
            SelectedItem="{Binding Path=SelectedListNameContactView />

            <Button Grid.Row="2"
                 Grid.Column="2"
                 Content="View Contacts"
                 VerticalAlignment="Center"
                 Margin="5" 
                 Width=" 100"
                Command="{Binding Path=ShowContactsCommand}"   
                CommandParameter="{Binding SelectedListNameContactView}"/>

</Grid>
<Grid HorizontalAlignment="Center"
      VerticalAlignment="Center"
      Margin="20"
      RenderTransformOrigin="1.25,2.662">

  <telerik:RadGridView x:Name="contactsGrid"
                       GroupRenderMode="Flat"
                       ItemsSource="{Binding Contacts}"
                       PropertyChanged="{Binding Path=SelectedListNameContactView}                                
                             AutoGenerateColumns="False"

                             CanUserFreezeColumns="False"
                             RowIndicatorVisibility="Collapsed"
                             CanUserResizeColumns="True"
                             Margin="5,5,5,5" 
                             IsReadOnly="True">
      <telerik:RadGridView.Columns>
      <telerik:GridViewDataColumn Header="First Name"
                                  DataMemberBinding="{Binding FirstName}"
                                  Width="100" />
      <telerik:GridViewDataColumn Header="Last Name"
                                  DataMemberBinding="{Binding LastName}"
                                  Width="100" />
      <telerik:GridViewDataColumn Header="Address"
                                  DataMemberBinding="{Binding Address}"
                                  Width="200" />
      <telerik:GridViewDataColumn Header="Address2"
                                  DataMemberBinding="{Binding Address2}"
                                  Width="75" />
      <telerik:GridViewDataColumn Header="City"
                                  DataMemberBinding="{Binding City}"
                                  Width="75" />
      <telerik:GridViewDataColumn Header="State"
                                  DataMemberBinding="{Binding State}" />
      <telerik:GridViewDataColumn Header="Zip"
                                  DataMemberBinding="{Binding ZipCode}"
                                  Width="75" />
      <telerik:GridViewDataColumn Header="Phone"
                                  DataMemberBinding="{Binding PhoneNumber}"
                                  Width="75" />
      <telerik:GridViewDataColumn Header="Media Group"
                                  DataMemberBinding="{Binding MediaGroup}"
                                  Width="100" />
      <telerik:GridViewDataColumn Header="Brochure Date"
                                  DataMemberBinding="{Binding BrochureDate}"
                                  Width="100" />
    </telerik:RadGridView.Columns>
  </telerik:RadGridView>

</Grid>


如果您使用交互性和交互库,您将能够在xaml中编写如下内容:

 <ComboBox HorizontalAlignment="Left" SelectedItem="{Binding SelectedComboBoxItem, Mode=TwoWay}" ItemsSource="{Binding ComboBoxItems}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding OnCopmboBoxSelectionChangedCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>
viewModel中的最后一个方法:

 private ICommand _onCopmboBoxSelectionChangedCommand;
 public ICommand OnCopmboBoxSelectionChangedCommand
 {
     get
     {
         if (_onCopmboBoxSelectionChangedCommand != null) return _onCopmboBoxSelectionChangedCommand;
         _onCopmboBoxSelectionChangedCommand = new RelayCommand(OnCopmboBoxSelectionChanged);
         return _onCopmboBoxSelectionChangedCommand;
     }
 }
 private void OnCopmboBoxSelectionChanged(object sender)
 { }

使用MVVM,对于这样的功能,我们使用视图模型,而不是视图。首先添加一个与
组合框
控件中的项类型相同的属性…为了简单起见,我假设它们是
字符串
值:

private string selectedItem = string.Empty;
...
public string SelectedItem
{
    get { return selectedItem; }
    set
    {
        selectedItem = value;
        NotifyPropertyChange("SelectedItem"); // << Implement INotifyPropertyChanged
        UpdateGrid();
    }
}
现在让我们看一下
UpdateGrid
方法:

private void UpdateGrid()
{
    // Update your property that is bound to the DataGrid.ItemsSource here
    Items = GetNewData(); // << You need to implement this method yourself
}
private void UpdateGrid()
{
//在此处更新绑定到DataGrid.ItemsSource的属性

Items=GetNewData();//这正是我设置它的方式。网格不填充,它返回记录。如果我将初始网格设置为显示与我的一个列表相关联的记录,它会很好地填充。从组合框中选择一些内容,它会命中代码,但网格不会更新。在winforms中,您可以进行刷新。不使用WPF。我发现了一个基本ly重新加载应用程序,该应用程序本质上是重新绘制的。除非我在这里找到更好的答案,否则我将尝试一下。这里是链接顺便说一句。好悲伤…我忘记为我的收藏引发OnPropertyChanged事件…感谢您的帮助。很高兴看到您坚持解决问题。如果您的问题现在已解决,请让其他用户知道知道是什么帮助解决了这个问题。
private void UpdateGrid()
{
    // Update your property that is bound to the DataGrid.ItemsSource here
    Items = GetNewData(); // << You need to implement this method yourself
}
<DataGrid ItemsSource="{Binding Items}" />
<ComboBox ItemsSource="{Binding YourCollectionNotShownHere}" 
    SelectedItem="{Binding SelectedItem}" />