WPF:UpdateSourceTrigger=PropertyChanged 问题与双值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26162784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: UpdateSourceTrigger=PropertyChanged issue with Double Values
提问by who_lee_oh
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.
我正在处理一个 WPF 项目,其中我有一个绑定到 ObservableCollection 的 DataGrid。这些值被正确限制,但我遇到的问题是我无法编辑具有双值的列。它不会让我在单元格中插入句点。
This is what I have for the XAML:
这就是我对 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));
}
}
}
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?
我怎样才能在 DataGrid 上做到这一点,如果我想更新列的值(假设我想更新 Field3 或任何具有双精度值的字段),我可以插入像 2.1 这样的双精度值?
Is a data template needed? Not to sure how to go about that, still a beginner.
是否需要数据模板?不知道如何去做,仍然是初学者。
Thanks for the help!
谢谢您的帮助!
回答by
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.
如果您真的想通过 PropertyChanged 触发器获得行为,您可以尝试使用 IsAsync=true 的 Binding,但我不确定这是正确的解决方案。
<DataGridTextColumn Header="Field 3" Binding="{Binding Field3, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:n\}, IsAsync=True}" />

