自定义控件中的数据网格未更新
本文关键字:网格 更新 数据网 数据 自定义控件 | 更新日期: 2023-09-27 17:56:44
在我的自定义控件中,它有一个 DataGrid 和两个按钮,一个用于在此 DataGrid 中添加行,另一个用于删除元素的按钮。
(由于我的声誉,我不能在这里发布图片,对不起! :-(
我的自定义控件代码隐藏:
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class CustonDatagrid : UserControl
{
public CustonDatagrid()
{
InitializeComponent();
}
#region DependencyProperty Content
/// <summary>
/// Registers a dependency property as backing store for the Content property
/// </summary>
public static readonly DependencyProperty ColectionProperty =
DependencyProperty.Register("Colection",
typeof(ObservableCollection<object>),
typeof(CustonDatagrid),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure));
/// <summary>
/// Gets or sets the Content.
/// </summary>
/// <value>The Content.</value>
public ObservableCollection<object> Colection
{
get { return (ObservableCollection<object>)GetValue(ColectionProperty); }
set { SetValue(ColectionProperty, value); }
}
#endregion
public static readonly RoutedEvent AddButtonEvent = EventManager.RegisterRoutedEvent(
"AddButtonClick",
RoutingStrategy.Bubble,
typeof (RoutedEventHandler),
typeof (CustonDatagrid));
public event RoutedEventHandler AddButtonClick
{
add { AddHandler(AddButtonEvent, value); }
remove { RemoveHandler(AddButtonEvent, value);}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var newEventArgs = new RoutedEventArgs(AddButtonEvent);
RaiseEvent(newEventArgs);
}
}
我的 .xaml:
<UserControl x:Class="WpfCustomControlLibrary1.CustonDatagrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Name="CustonDataGrid">
<Grid>
<DockPanel LastChildFill="True" >
<StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" Orientation="Horizontal">
<Button Margin="5" Width="20" Click="ButtonBase_OnClick" >+</Button>
<Button Margin="5" Width="20">-</Button>
</StackPanel>
<DataGrid ItemsSource="{Binding ElementName=CustonDataGrid, Path=Colection}" DockPanel.Dock="Top"></DataGrid>
</DockPanel>
</Grid>
</UserControl>
以及 wpf 窗口中的用法:
XAML 代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfCustomControlLibrary1="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=Model}"
>
<Grid>
<wpfCustomControlLibrary1:CustonDatagrid Colection="{Binding Path=Colection}" AddButtonClick="CustonDatagrid_OnAddButtonClick">
</wpfCustomControlLibrary1:CustonDatagrid>
</Grid>
</Window>
而背后的代码+视图模型+数据网格行视图模型:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
Model = new Model();
InitializeComponent();
}
public Model Model { get; set; }
private void CustonDatagrid_OnAddButtonClick(object sender, RoutedEventArgs e)
{
Model.AddElement();
}
}
public class Model : INotifyPropertyChanged
{
public ObservableCollection<DataGridRowModel> Colection { get; set; }
public void AddElement()
{
if (Colection == null) Colection = new ObservableCollection<DataGridRowModel>();
Colection.Add( new DataGridRowModel()
{
Name = "Test"
});
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DataGridRowModel
{
public string Name { get; set; }
}
我遇到的问题是数据网格没有显示添加到集合中的新元素。调试时,我可以看到我的集合包含许多元素(每次单击 (+) 按钮时一个),但这些元素不会显示在视图中。
有人可以给我一个提示,我犯了错误或(可能)丢失了代码吗?!?
谢谢。
你在CustonDatagrid.xaml
中犯了一个小错误
<DataGrid ItemsSource="{Binding ElementName=CustonDataGrid, Path=Colection}" DockPanel.Dock="Top"></DataGrid>
没有称为CustonDataGrid
的元素,因此元素从未被反映。
将其更改为
<DataGrid ItemsSource="{Binding Path=Colection}"></DataGrid>
我还对您的MainWindow.cs
进行了小改动
public partial class MainWindow : Window
{
public MainWindow()
{
Model = new Model();
InitializeComponent();
this.DataContext = Model;
}
public Model Model { get; set; }
private void CustonDatagrid_OnAddButtonClick(object sender, RoutedEventArgs e)
{
Model.AddElement();
}
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfCustomControlLibrary1="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
Title="MainWindow" Height="350" Width="525">
<ScrollViewer>
<wpfCustomControlLibrary1:CustonDatagrid Colection="{Binding Colection,Mode=TwoWay}" AddButtonClick="CustonDatagrid_OnAddButtonClick">
</wpfCustomControlLibrary1:CustonDatagrid>
</ScrollViewer>
</Window>
添加了用于Model.cs
的构造函数
public class Model : INotifyPropertyChanged
{
public ObservableCollection<DataGridRowModel> Colection { get; set; }
public Model()
{
Colection = new ObservableCollection<DataGridRowModel>();
}
public void AddElement()
{
Colection.Add(new DataGridRowModel { Name = "Test" });
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
我希望它也能在你的结局中起作用。