如果Datagrid中存在相关记录,则下拉项应以粗体显示
本文关键字:显示 存在 Datagrid 记录 如果 | 更新日期: 2023-09-27 18:07:16
我正在研究wpf MVVM应用程序,有2个控件组合框和数据网格。根据在"组合框"中选择的值,应该过滤Datagrid项目源。这工作得很好。下一个要求是,如果组合框中的项目在Datagrid中至少有一个过滤值,那么组合框中的项目文本应该设置为粗体。谁能建议以什么优化的方式来实现这个要求?
- 创建
Attached
属性以获得ItemsSource of DataGrid
及其PropertyChangedCallback()
。 - 将此属性应用于用于显示查找项的控件。它可以是
TextBlock
,显示ComboBox
中的国家名称。 - 您将获得
DataGrid.ItemsSource
和TextBlock
的回调调用,只需循环遍历列表,并将FontWeight
设置为Bold
。
示例:
Window2.xaml
<Window x:Class="WpfAdvanced.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:WpfAdvanced"
xmlns:local="clr-namespace:WpfAdvanced"
Title="Window2" Height="300" Width="300">
<StackPanel x:Name="Grd">
<Button Content="Add" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<ComboBox x:Name="Cmb">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" local:Window2.ItemsSource="{Binding ItemsSource, ElementName=Dgrd}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid x:Name="Dgrd">
</DataGrid>
</StackPanel>
</Window>
Window2.xaml.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfAdvanced
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
DataGrid b = new DataGrid();
ArrayList countries = new ArrayList();
ArrayList lookupCountries = new ArrayList();
public static ArrayList GetItemsSource(DependencyObject obj)
{
return (ArrayList)obj.GetValue(ItemsSourceProperty);
}
public static void SetItemsSource(DependencyObject obj, ArrayList value)
{
obj.SetValue(ItemsSourceProperty, value);
}
// Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.RegisterAttached("ItemsSource", typeof(ArrayList), typeof(Window2), new PropertyMetadata(null, new PropertyChangedCallback(myCallback)));
private static void myCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBlock tb = (TextBlock)d;
ArrayList v = (ArrayList)e.NewValue;
bool found = false;
for (int i = 0; i < v.Count; ++i)
{
dynamic o = v[i];
if (o.Name == tb.Text)
{
found = true;
break;
}
}
if (found)
tb.FontWeight = FontWeights.ExtraBold;
}
public Window2()
{
InitializeComponent();
dynamic o1 = new { Name = "UK", Capital = "London" };
countries.Add(o1);
dynamic o2 = new { Name = "USA", Capital = "New York" };
countries.Add(o2);
dynamic o3 = new { Name = "INDIA", Capital = "New Delhi" };
countries.Add(o3);
dynamic o4 = new { Name = "INDIA", Capital = "Shimla" };
countries.Add(o4);
Dgrd.ItemsSource = countries;
dynamic c1 = new { Name = "UK" };
lookupCountries.Add(c1);
dynamic c2 = new { Name = "INDIA" };
lookupCountries.Add(c2);
dynamic c3 = new { Name = "SRILANKA" };
lookupCountries.Add(c3);
Cmb.ItemsSource = lookupCountries;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
CollectionViewSource.GetDefaultView(Dgrd.ItemsSource).Filter = grdFilter;
}
private bool grdFilter(dynamic obj)
{
dynamic i = Cmb.SelectedItem;
return (obj.Name == i.Name);
}
}
}