基于Kendo GridViewColumn创建WPF Telerik自定义网格列求助
Got it, let's walk through finishing your custom MultiDropDownColumn step by step. I'll cover the column implementation, XAML setup, and ViewModel adjustments to make everything work seamlessly.
1. Finalize the Custom MultiDropDownColumn Class
First, let's polish your MultiDropDownColumn to support multi-select functionality properly. We'll add necessary dependency properties and ensure the cell element binds correctly to your data:
using Telerik.Windows.Controls; using Telerik.Windows.Controls.GridView; using System.Windows; public class MultiDropDownColumn : GridViewBoundColumnBase { // Dependency property for dropdown options public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(MultiDropDownColumn), new PropertyMetadata(null)); // Dependency property for selected items (multi-select) public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(nameof(SelectedItems), typeof(object), typeof(MultiDropDownColumn), new PropertyMetadata(null)); public object ItemsSource { get => GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } public object SelectedItems { get => GetValue(SelectedItemsProperty); set => SetValue(SelectedItemsProperty, value); } // Render the dropdown in view mode public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) { var multiDropDown = new RadMultiColumnComboBox(); // Bind dropdown options to the column's ItemsSource multiDropDown.SetBinding(RadMultiColumnComboBox.ItemsSourceProperty, new System.Windows.Data.Binding(nameof(ItemsSource)) { Source = this }); // Bind selected items to the data item's property (from DataMemberBinding) multiDropDown.SetBinding(RadMultiColumnComboBox.SelectedItemsProperty, this.DataMemberBinding); // Configure multi-select behavior multiDropDown.IsEditable = false; multiDropDown.SelectionMode = Telerik.Windows.Controls.SelectionMode.Multiple; multiDropDown.DisplayMemberPath = "Name"; // Set to your contest model's display property return multiDropDown; } // Optional: Auto-open dropdown when entering edit mode protected override FrameworkElement PrepareCellForEdit(GridViewCell cell, RoutedEventArgs editingEventArgs) { var multiDropDown = cell.Content as RadMultiColumnComboBox; multiDropDown?.IsDropDownOpen = true; return base.PrepareCellForEdit(cell, editingEventArgs); } // Tell the grid the data type for filtering/sorting public override System.Type GetColumnDataType() { return typeof(System.Collections.IEnumerable); } }
Key details:
- Added
ItemsSourceto bind yourCmbContestcollection directly to the column - Used Telerik's native
RadMultiColumnComboBoxfor built-in multi-select support - Linked the dropdown's selected items to your data item's property via
DataMemberBinding
2. Update Your XAML Configuration
Now use the custom column in your RadGridView, making sure to bind all required properties:
<telerik:RadGridView x:Name="ContestGridView" ItemsSource="{Binding YourGridData}"> <telerik:RadGridView.Columns> <!-- Regular data column example --> <telerik:GridViewDataColumn Header="Item Name" DataMemberBinding="{Binding ItemName}" /> <!-- Custom MultiDropDownColumn --> <local:MultiDropDownColumn Header="Selected Contests" DataMemberBinding="{Binding SelectedContests}" ItemsSource="{Binding CmbContest}" /> </telerik:RadGridView.Columns> </telerik:RadGridView>
Replace local: with your actual namespace where the MultiDropDownColumn is defined.
3. Adjust Your ViewModel
Ensure your ViewModel has the necessary collections and properties to support the custom column:
using System.Collections.ObjectModel; using Telerik.Windows.Controls; public class MainViewModel : ViewModelBase { // Collection of contests for dropdown options public ObservableCollection<Contest> CmbContest { get; set; } // Collection of items displayed in the grid public ObservableCollection<GridItem> YourGridData { get; set; } public MainViewModel() { // Initialize collections CmbContest = new ObservableCollection<Contest>(); YourGridData = new ObservableCollection<GridItem>(); // Populate sample data CmbContest.Add(new Contest { Id = 1, Name = "Spring Contest" }); CmbContest.Add(new Contest { Id = 2, Name = "Summer Contest" }); CmbContest.Add(new Contest { Id = 3, Name = "Fall Contest" }); YourGridData.Add(new GridItem { ItemName = "Entry 1", SelectedContests = new ObservableCollection<Contest>() }); } } // Grid data item class public class GridItem : ViewModelBase { public string ItemName { get; set; } private ObservableCollection<Contest> _selectedContests; public ObservableCollection<Contest> SelectedContests { get => _selectedContests; set { _selectedContests = value; OnPropertyChanged(); } } } // Contest model class public class Contest { public int Id { get; set; } public string Name { get; set; } }
Quick Troubleshooting Tips
- If dropdown items don't appear: Verify the
ItemsSourcebinding on the custom column points to yourCmbContestcollection - If selections don't save: Ensure your
SelectedContestsproperty usesObservableCollection(or implementsINotifyCollectionChanged) - To customize dropdown columns: Add
RadMultiColumnComboBox.Columnsin theCreateCellElementmethod to define custom grid columns for the dropdown
内容的提问来源于stack exchange,提问作者MikkaRin

