私はテキストボックスとデータグリッドを持っています。 DataGridには、名前と電子メールアドレスの2つの列があります。私は、テキストボックスの値でデータグリッド値をフィルタリングしたい。 フィルタwpfのデータグリッドの値をテキストボックス
5
A
答えて
22
あなたは、あなたがFilter
述語を適用し、必要なときにリストをrefeshできDataGrid
ItemSource
ためICollectionView
を使用することができます。
ここは非常に簡単な例です。
XAML:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="188" Width="288" Name="UI" >
<StackPanel DataContext="{Binding ElementName=UI}">
<TextBox Text="{Binding FilterString, UpdateSourceTrigger=PropertyChanged}" />
<DataGrid ItemsSource="{Binding DataGridCollection}" />
</StackPanel>
</Window>
コード:
namespace WpfApplication10
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ICollectionView _dataGridCollection;
private string _filterString;
public MainWindow()
{
InitializeComponent();
DataGridCollection = CollectionViewSource.GetDefaultView(TestData);
DataGridCollection.Filter = new Predicate<object>(Filter);
}
public ICollectionView DataGridCollection
{
get { return _dataGridCollection; }
set { _dataGridCollection = value; NotifyPropertyChanged("DataGridCollection"); }
}
public string FilterString
{
get { return _filterString; }
set
{
_filterString = value;
NotifyPropertyChanged("FilterString");
FilterCollection();
}
}
private void FilterCollection()
{
if (_dataGridCollection != null)
{
_dataGridCollection.Refresh();
}
}
public bool Filter(object obj)
{
var data = obj as TestClass;
if (data != null)
{
if (!string.IsNullOrEmpty(_filterString))
{
return data.Name.Contains(_filterString) || data.Email.Contains(_filterString);
}
return true;
}
return false;
}
public IEnumerable<TestClass> TestData
{
get
{
yield return new TestClass { Name = "1", Email = "[email protected]" };
yield return new TestClass { Name = "2", Email = "[email protected]" };
yield return new TestClass { Name = "3", Email = "[email protected]" };
yield return new TestClass { Name = "4", Email = "[email protected]" };
yield return new TestClass { Name = "5", Email = "[email protected]" };
yield return new TestClass { Name = "6", Email = "[email protected]" };
yield return new TestClass { Name = "7", Email = "[email protected]" };
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class TestClass
{
public string Name { get; set; }
public string Email { get; set; }
}
}
結果:どのカラム、名前または電子メールで
関連する問題
- 1. wpfのデータグリッド値をクリア
- 2. WPFでデータグリッドの値を読み取る
- 3. データグリッドの行フィルタは、
- 4. WPFデータグリッド
- 5. WPFデータグリッド
- 6. WPF-xamlテキストボックス値の合計
- 7. WPFの合計値はテキストボックス
- 8. WPFバインディングクエリ - データグリッドのトグルボタン
- 9. WPFデータグリッドSyling
- 10. WPFデータグリッドrowHeaderオブジェクト
- 11. wpfデータグリッド集約
- 12. WPFデータグリッド、行
- 13. WPFデータグリッド - DataGrid内
- 14. C#のWPFのデータグリッドのデータバインディングエラー
- 15. データグリッド列から複数のセル値を単一のテキストボックスにバインド
- 16. WPF - データグリッド内のオブジェクトのデータテンプレート
- 17. WPFのデータグリッド通貨のフォーマットコンバーター
- 18. WPFデータグリッドとカスタムカラムのバインディング
- 19. ネストされたWPFのデータグリッド
- 20. WPFリストビュー/データグリッド内のボタン
- 21. WPF C#、バインド後のデータグリッド列
- 22. WPFデータグリッドの空行下
- 23. wpfデータグリッド行の小計セル
- 24. WPFテキストボックスをデータグリッドの現在選択されている行にバインドします
- 25. WPFでのテキストボックス値のフォーマット方法
- 26. wpf:テキストボックスのMousedoubleclick
- 27. データグリッドのフォーマット値
- 28. WPFデータグリッドでsp_who2の出力を表示
- 29. wpfデータグリッドの列をフリーズする
- 30. WPFデータグリッドのdouble tabingを取り除く
?そしてここでMVVMデザインパターンを使用していますか? – Colin
@Colin、MVVMでこれを行う方法 – Mussammil