ToB企服应用市场:ToB评测及商务社交产业平台
标题:
wpf datagrid 实现双向绑定
[打印本页]
作者:
愛在花開的季節
时间:
2024-8-22 07:44
标题:
wpf datagrid 实现双向绑定
前台
<DataGrid
AutoGenerateColumns="False"
Background="White"
CanUserAddRows="True"
Grid.Row="1"
RowEditEnding="DataGrid_OnRowEditEnding"
RowHeight="60"
SelectionUnit="CellOrRowHeader"
x:Name="DataGrid">
<!-- Mode=TwoWay, UpdateSourceTrigger=PropertyChanged 这两个设置很重要,没有的话无法实现双向绑定-->
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding ZhuJieNeiRong, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Header="注解内容"
Width="*">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
<!-- -->
<!-- 这里的功能是 如果设定了行高,加上这个样式 编辑表格时 内容是一直垂直居中-->
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
复制代码
背景
public static ObservableCollection<Zjwz> _gridZhuJieList = new ObservableCollection<Zjwz>();
private void win_OnLoaded(object sender, RoutedEventArgs e)
{
DataGrid.ItemsSource = _gridZhuJieList;
}
复制代码
数据对象
public class Zjwz : NotificationObject
{
private string _zhuJieNeiRong;
public string ZhuJieNeiRong
{
get { return _zhuJieNeiRong; }
set
{
_zhuJieNeiRong = value;
RaisePropertyChanged();
}
}
}
复制代码
数据对象须要的类
using System.Collections.Generic;
using System.ComponentModel;
namespace CommonSql
{
public class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged( string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual bool SetProperty<T>(ref T member, T value, string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(member, value))
{
return false;
}
member = value;
RaisePropertyChanged(propertyName);
return true;
}
}
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4