Abh*_*Net 6 c# wpf xaml binding silverlight-4.0
我有一个DataTemplate
2列的列DatePickers
绑定到2个属性.当更改这些控件中的数据时,仅更新最后一个控件
<sdk:DataGridTemplateColumn Width="300" CanUserReorder="False" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid MouseRightButtonDown="ActionsGrid_MouseRightButtonDown" Width="300" Height="40" MouseLeftButtonDown="ActionsGrid_MouseLeftButtonDown">
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Stretch" Width="100" Text="{Binding Start, Converter={StaticResource DateConverter}}"
Padding="2" HorizontalAlignment="Center" />
<TextBlock VerticalAlignment="Stretch" Width="100" Text="{Binding Due, Converter={StaticResource DateConverter}}"
Padding="2" HorizontalAlignment="Center" />
</StackPanel>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Start, Mode=TwoWay,}" Padding="2" />
<sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Due, Mode=TwoWay, ValidatesOnDataErrors=True}" Padding="2" />
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果我更新Start和Due Only Due更新.此外绑定工作正常,因为如果我在我的Model类中的Start上放置一个breakPoint它会被命中但是传递的值是Start的原始值
编辑1
经过一些调试后我发现如果我内部只有一个控件DataTemplate
就可以了.当我改变日期时,断点也会立刻被击中.但是如果我有多个控件,那么在我从列中聚焦之后才会触发断点,然后只有最后一个绑定工作.
编辑2
经过一些mroe调试后,我注意到如果我只使用CellTemplate
并丢弃单元格它会正常工作EditTemplate
<sdk:DataGridTemplateColumn Width="300" CanUserReorder="False" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Start, Mode=TwoWay,}" Padding="2" />
<sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Due, Mode=TwoWay, ValidatesOnDataErrors=True}" Padding="2" />
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
编辑3
private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
(sender as DatePicker).GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();
}
Run Code Online (Sandbox Code Playgroud)
我能够使用selectedDatechange
事件刷新控件的绑定,然后刷新发件人的绑定.
我仍然不确定为什么双向绑定工作不起作用.
任何人都可以解释为什么会这样吗?
编辑4
模型和属性
public DateTime? Start
{
get { return _Start; }
set
{
_Start = value; Dirty = true;
if (_Start.HasValue && _Due.HasValue && _Start.Value > _Due.Value)
_dataErrors["Start"] = "Start date cannot be greater than the Due date";
else
if (_dataErrors.ContainsKey("Start"))
_dataErrors.Remove("Start");
NotifyPropertyChanged(); NotifyPropertyChanged("CalcStatus");
}
}
public DateTime? Due
{
get { return _Due; }
set
{
_Due = value; Dirty = true;
if (_Start.HasValue && _Due.HasValue && _Start.Value > _Due.Value)
_dataErrors["Start"] = "Start date cannot be greater than the Due date";
else
if (_dataErrors.ContainsKey("Start"))
_dataErrors.Remove("Start");
NotifyPropertyChanged("Due"); NotifyPropertyChanged("CalcStatus");
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,我想我终于修好了。我仍然不能 100% 确定它为什么不起作用,但我将创建一个新的小项目并尝试找出原因。
发生的事情是我NoitifyPropertyChangedEvent
通过传递它来修改 my [CallerMemberName]
,以便自动传递调用它的属性的名称。这意味着如果我更改名称属性(例如“Start”),我不必担心更新NotifyPropertyChanged("Start")
。
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName =null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Run Code Online (Sandbox Code Playgroud)
这对于所有属性都适用,除了当我有DataTemplateColumn
. 将其转换回标准代码并明确传递 propertyName 解决了我的问题。
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
207 次 |
最近记录: |