WPF:UpdateSourceTrigger=PropertyChanged 问题与双值 [英] WPF: UpdateSourceTrigger=PropertyChanged issue with Double Values
问题描述
我正在处理一个 WPF 项目,其中我有一个绑定到 ObservableCollection 的 DataGrid.这些值被正确限制,但我遇到的问题是我无法编辑具有双值的列.它不会让我在单元格中插入句点.
I'm working on a WPF project where I have a DataGrid bounded to a ObservableCollection. The values are being bounded properly but the issue that I am running into is that I can't edit the columns with double values. It will not let me insert a period into the cell.
这就是我对 XAML 的看法:
This is what I have for the XAML:
<DataGrid Name="dataGrid" AutoGenerateColumns="False"
CanUserResizeColumns="True" CanUserAddRows="False" CanUserSortColumns="True" ItemsSource="{Binding}"
HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
ColumnWidth="*" Margin="0,51,186,58"
RowEditEnding="dataGrid_RowEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Field 1" Binding="{Binding Field1, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Field 2" Binding="{Binding Field2, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Field 3" Binding="{Binding Field3, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Field 4" Binding="{Binding Field4, UpdateSourceTrigger=PropertyChanged}" />
<DataGridCheckBoxColumn Header="Field 5" Binding="{Binding Field5, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Field 6" Binding="{Binding Field6, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
<DataGrid>
这是类(抱歉奇怪的类属性:/我是故意这样做的)
And here is the class (Sorry for the weird class properties :/ I made it that way on purpose)
class FieldClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _field1;
public int Field1
{
get { return _field1; }
set
{
_field1 = value;
OnPropertyChanged("Field1");
}
}
private int _field2;
public int Field2
{
get { return _field2; }
set
{
_field2 = value;
OnPropertyChanged("Field2");
}
}
private double _field3;
public double Field3
{
get { return _field3; }
set
{
_field3 = value;
OnPropertyChanged("Field3");
}
}
private double _field4;
public double Field4
{
get { return _field4; }
set
{
_field4 = value;
OnPropertyChanged("Field4");
}
}
private bool _field5;
public bool Field5
{
get { return _field5; }
set
{
_field5 = value;
OnPropertyChanged("Field5");
}
}
private double _field6;
public double Field6
{
get { return _field6; }
set
{
_field6 = value;
OnPropertyChanged("Field6");
}
}
public FieldClass()
{
_field1 = 0;
_field2 = 0;
_field3 = 0.0;
_field4 = 0.0;
_field5 = false;
_field6 = 0.0;
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
如何在 DataGrid 上做到这一点,如果我想更新列的值(假设我想更新 Field3 或任何具有双精度值的字段),我可以插入像 2.1 这样的双精度值?
How can I make it so on the DataGrid, if I wanted to update the value of a column (lets say i want to update Field3 or any field that has a double), I can insert a double value like 2.1?
是否需要数据模板?不知道如何去做,仍然是初学者.
Is a data template needed? Not to sure how to go about that, still a beginner.
感谢您的帮助!
推荐答案
如果您真的想通过 PropertyChanged 触发器获得行为,您可以尝试使用 IsAsync=true 的 Binding,但我不确定这是正确的解决方案.
If you really want to obtain the behavior with PropertyChanged trigger you can try to use IsAsync=true of Binding, but I'm not sure that's the correct solution.
<DataGridTextColumn Header="Field 3" Binding="{Binding Field3, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:n\}, IsAsync=True}" />
这篇关于WPF:UpdateSourceTrigger=PropertyChanged 问题与双值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!