我在DataGridTemplateColumn中使用WPF工具包的DateTimeUpDown(在常规版本中不是,它仅在最新的代码更改中可用)。我已经将其绑定到我的一个类的DateTime属性中,该类又将DataGrid绑定到所述类的ObservableCollection。除了包括DateTimeUpDown控件的列之外,DataGrid中的每一列。我的类像ObservableCollection本身一样实现INotifyPropertyChanged。另外,我覆盖了ObservableCollection的Insert,因此我钩住了每个添加的对象的NotifyPropertyChanged委托,因此我每次更改都会更新后端数据库。我的问题是...即使我使用控件更改日期,也永远不会解雇该代表。有任何想法吗?
XAML:
<Window x:Class="LDary.Deudas"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
Title="Deudas" Height="300" Width="300" Name="WinDeudas" Closing="WinDeudas_Closing">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" Name="Lista" AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WinDeudas, Path=SDeudas}"
GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeRows="False" IsTextSearchEnabled="True" IsReadOnly="False"
AlternatingRowBackground="#FFF0F0F0" BorderBrush="#FFF0F0F0"
ScrollViewer.HorizontalScrollBarVisibility="Hidden" RowHeaderWidth="0">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}" Width="*" IsReadOnly="True"/>
<DataGridTextColumn Header="Cedula" Binding="{Binding Cedula}" IsReadOnly="True"/>
<DataGridTextColumn Header="Telefono" Binding="{Binding Telefono}" IsReadOnly="True"/>
<DataGridTextColumn Header="Caracteristica" Binding="{Binding Caracteristica}" IsReadOnly="True"/>
<DataGridTextColumn Header="Fecha" Binding="{Binding Fecha}" IsReadOnly="True"/>
<!--<DataGridTextColumn Header="Fecha Limite" Binding="{Binding FechaPago}" />-->
<DataGridTemplateColumn Header="Fecha Limite" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<t:DateTimeUpDown Value="{Binding FechaPago}" Format="LongDate"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Valor" Binding="{Binding Valor}" />
</DataGrid.Columns>
</DataGrid>
<TextBox Name="txtBusqueda" Grid.Row="1" Padding="0,5"/>
<Button Name="BtnGuardar" Grid.Row="2" Padding="0,5" Content="Guardar Cambios" Click="BtnGuardar_Click"/>
</Grid>
码:
public new void Insert(int index, Deuda Item)
{
Item.PropertyChanged += new PropertyChangedEventHandler(PropertyChangedHandler);
base.Insert(index, Item);
}
提前致谢。
最佳答案
加UpdateSourceTrigger=PropertyChanged
到您的DateTimeUpDown控件绑定,像这样
<t:DateTimeUpDown Value="{Binding FechaPago, UpdateSourceTrigger=PropertyChanged}" Format="LongDate"/>
关于c# - WPF DateTimeUpDown绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4532257/