如果Datagrid中存在相关记录,则下拉项应为粗体

时间:2016-09-17 04:25:01

标签: c# .net wpf mvvm data-binding

我正在研究wpf MVVM应用程序,有2个控件组合框和Datagrid。根据在Combobox中选择的值,应过滤Datagrid项目源。这工作正常。 下一个要求是,如果Combobox中的项目在Datagrid中至少有一个过滤值,则Combobox中的项目文本应设置为粗体。 任何人都可以建议以这种要求的优化方式实施

1 个答案:

答案 0 :(得分:0)

  1. 使用Attached创建ItemsSource of DataGrid属性以获取PropertyChangedCallback()
  2. 将此属性应用于用于显示查找项的控件。可以TextBlockComboBox中显示国家/地区名称。
  3. 您将通过DataGrid.ItemsSourceTextBlock收到回叫,只需遍历列表,然后将FontWeight设置为Bold
  4. 示例:

    <强> 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);
            }
        }   
    }