C# 根据组合框选择从SQL Server筛选Listview

C# 根据组合框选择从SQL Server筛选Listview,c#,sql-server,wpf,listview,combobox,C#,Sql Server,Wpf,Listview,Combobox,如何从组合框选择示例中筛选出listview 如果用户在组合框中选择项目5*108,则列表视图将显示p.krydsmål C# 组合框 <ComboBox x:Name="comboBox_Copy" Width="150" Height="40" Foreground="#FF00FB0B" Background="#FF303030" FontSize="16" Canvas.Left="1030" Canvas.Top="24" Style="{StaticResource Com

如何从组合框选择示例中筛选出listview

如果用户在组合框中选择项目
5*108
,则列表视图将显示
p.krydsmål

C#

组合框

<ComboBox x:Name="comboBox_Copy" Width="150" Height="40" Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  Canvas.Left="1030" Canvas.Top="24" Style="{StaticResource ComboBoxTest2}"  SelectedItem = "{Binding SelectedParam, ElementName=win, UpdateSourceTrigger=PropertyChanged}">
                        <ComboBoxItem Content="Ingen"/>
                        <ComboBoxItem Content="3x98"/>
                        <ComboBoxItem Content="3x112"/>
                        <ComboBoxItem Content="4x98"/>
                        <ComboBoxItem Content="4x100"/>
                        <ComboBoxItem Content="4x108"/>
                        <ComboBoxItem Content="4x114.3"/>
                        <ComboBoxItem Content="4x160"/>
                        <ComboBoxItem Content="5x98"/>
                        <ComboBoxItem Content="5x100"/>
                        <ComboBoxItem Content="5x108"/>

我建议使用MVVM模式,让事情变得更简单。但当您使用代码隐藏时:

1-在窗口上实现InotifyProperty更改:

public class MainWindow: Window, INotifyPropertyChanged
{
  //Existing code....


  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(String info)
  {
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
  }
}
2-在组合框及其setter中定义表示选定项的属性,可以触发筛选方法:

private string _selectedParam;
public string SelectedParam 
{
  get{return _selectedParam;}
  set
  {
     _selectedParam = value;  
     NotifyPropertyChanged("SelectedParam");
     hjuldata.ItemsSource = FilterKategori(_selectedParam).Tables[0].DefaultView;

  }
}
3-将组合框SelectedItem绑定到新属性:

<ComboBox x:Name="comboBox_Copy" 
          SelectedItem = "{Binding SelectedParam, ElementName=YourWindowsName, UpdateSourceTrigger=PropertyChanged}"
          Width="150" Height="40" 
          Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  
          Canvas.Left="1030" Canvas.Top="24">
     <ComboBox.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF303030" />
     </ComboBox.Resources>
     <ComboBoxItem Content="Ingen"/>
     <ComboBoxItem Content="3*98"/>
     <ComboBoxItem Content="3*112"/>
</ComboBox>

我建议使用MVVM模式,使事情变得更简单。但当您使用代码隐藏时:

1-在窗口上实现InotifyProperty更改:

public class MainWindow: Window, INotifyPropertyChanged
{
  //Existing code....


  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(String info)
  {
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
  }
}
2-在组合框及其setter中定义表示选定项的属性,可以触发筛选方法:

private string _selectedParam;
public string SelectedParam 
{
  get{return _selectedParam;}
  set
  {
     _selectedParam = value;  
     NotifyPropertyChanged("SelectedParam");
     hjuldata.ItemsSource = FilterKategori(_selectedParam).Tables[0].DefaultView;

  }
}
3-将组合框SelectedItem绑定到新属性:

<ComboBox x:Name="comboBox_Copy" 
          SelectedItem = "{Binding SelectedParam, ElementName=YourWindowsName, UpdateSourceTrigger=PropertyChanged}"
          Width="150" Height="40" 
          Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  
          Canvas.Left="1030" Canvas.Top="24">
     <ComboBox.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF303030" />
     </ComboBox.Resources>
     <ComboBoxItem Content="Ingen"/>
     <ComboBoxItem Content="3*98"/>
     <ComboBoxItem Content="3*112"/>
</ComboBox>

现在您已经显示了SelectedParam的调试值,我发现了问题所在。问题在组合框中。您的组合框应该类似于:

<ComboBox x:Name="comboBox_Copy" Width="150" Height="40" Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  
       SelectedValuePath="Content"
       SelectedValue = "{Binding SelectedParam, ElementName=win, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem Content="Ingen"/>
            <ComboBoxItem Content="3x98"/>
            <ComboBoxItem Content="3x112"/>
            <ComboBoxItem Content="4x98"/>
            <ComboBoxItem Content="4x100"/>
            <ComboBoxItem Content="4x108"/>
            <ComboBoxItem Content="4x114.3"/>
            <ComboBoxItem Content="4x160"/>
            <ComboBoxItem Content="5x98"/>
            <ComboBoxItem Content="5x100"/>
            <ComboBoxItem Content="5x108"/>

</ComboBox>

请注意SelectedValuePath。这将向SelectedParam发送字符串,而不是对象ComboBoxItem

在my github中查看演示:


请注意名称,我的可能与您的不同…

现在您已显示SelectedParam的调试值,我遇到了问题。问题在组合框中。您的组合框应该类似于:

<ComboBox x:Name="comboBox_Copy" Width="150" Height="40" Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  
       SelectedValuePath="Content"
       SelectedValue = "{Binding SelectedParam, ElementName=win, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem Content="Ingen"/>
            <ComboBoxItem Content="3x98"/>
            <ComboBoxItem Content="3x112"/>
            <ComboBoxItem Content="4x98"/>
            <ComboBoxItem Content="4x100"/>
            <ComboBoxItem Content="4x108"/>
            <ComboBoxItem Content="4x114.3"/>
            <ComboBoxItem Content="4x160"/>
            <ComboBoxItem Content="5x98"/>
            <ComboBoxItem Content="5x100"/>
            <ComboBoxItem Content="5x108"/>

</ComboBox>

请注意SelectedValuePath。这将向SelectedParam发送字符串,而不是对象ComboBoxItem

在my github中查看演示:



请注意名称,我的可能与你的不同…

似乎这可能是解决方案当选择ingen时,我该如何制作它,然后它将显示所有项目而不是对其进行过滤,如果我不想从另一个组合框中对其进行更多过滤,“ingen”表示“所有”如何?可以只需验证所选内容:公共字符串SelectedParam{get{return}SelectedParam;}set{SelectedParam=value;NotifyPropertyChanged(“SelectedParam”);if({SelectedParam==“ingen”){BindData();}else{hjuldata.ItemsSource=FilterKategori(_SelectedParam).Tables[0]。DefaultView;}}现在,关于筛选,即使对于,您也可以使用相同的原则:在“MainWindow”类型的声明中将public partial class更改为public class Missing partial修饰符时出错;这种类型的另一个部分声明存在,并且方法FilterKategori的无重载接受1个参数,我已经用我所做的更新了我的问题!对不起,我打错了窗口声明。您不需要删除“部分”。。。正如我所说,您必须实现INotifyPropertyChanged:public部分类MainWindow:Window,INotifyPropertyChangedseems可能是解决方案当选择ingen时,我该如何制作它,然后它将显示所有项目,而不是过滤它,如果我不想从另一个组合框中过滤更多,那么多重过滤又如何呢?ingen表示“全部”?可以只需验证所选内容:公共字符串SelectedParam{get{return}SelectedParam;}set{SelectedParam=value;NotifyPropertyChanged(“SelectedParam”);if({SelectedParam==“ingen”){BindData();}else{hjuldata.ItemsSource=FilterKategori(_SelectedParam).Tables[0]。DefaultView;}}现在,关于筛选,即使对于,您也可以使用相同的原则:在“MainWindow”类型的声明中将public partial class更改为public class Missing partial修饰符时出错;这种类型的另一个部分声明存在,并且方法FilterKategori的无重载接受1个参数,我已经用我所做的更新了我的问题!对不起,我打错了窗口声明。您不需要删除“部分”。。。正如我所说,您必须实现INotifyPropertyChanged:public部分类MainWindow:Window,InotifyPropertyChangedThat起作用了,所以现在我只需要弄清楚如何使分组与此一起工作,但至少它解决了我的问题。当我尝试使用分组异常抛出时,它给了我这个问题:System.Data.SqlClient.SqlException中的System.Data.dll附加信息:参数化查询'(@param1 nvarchar(4000))选择ps.Mærket作为Mærke,P.DataID,P.B'需要未提供的参数'@param1'。此消息M'需要未提供的参数'@param1',表示您在SQL语句中定义了一个参数,但是您没有为它提供值。是的,SelectedParam中的值为null不确定如何修复这是我必须在sql管理中提供的吗?这是有效的,所以现在我只需要找出如何使分组使用它,但至少它解决了我的问题。当我尝试使用分组时,请给我这个引发异常:System.Data.dll中的“System.Data.SqlClient.SqlException”其他信息:参数化查询“(@param1 nvarchar(4000))选择ps.Mærket作为Mærke,P.DataID,P.B”需要未提供的参数“@param1”。此消息“需要未提供的参数”@param1”表示您已收到