我无法使用DataGrid.Resources使TwoWay Binding正常工作。将出现TextBox,其中包含编辑行。当我退出该行时,绑定到该行的对象不会使用新值更新(使用实现INotifyPropertyChanged的对象上的断点进行测试)。
在构建DataGrid.Resources的之外的多个Datatemplates 之前,数据绑定工作正常。
<DataGrid AutoGenerateColumns="False" Name="dataGridProviderProperties" Height="215" FontSize="11" HorizontalGridLinesBrush="#FFC1C1C1"
VerticalGridLinesBrush="#FFC1C1C1" Padding="0" Background="#00000000" EnableRowVirtualization="False"
HorizontalScrollBarVisibility="Auto" EnableColumnVirtualization="True" CanUserResizeRows="False" AlternationCount="2"
AlternatingRowBackground="#05000000" CanUserAddRows="False" DataContext="{Binding}" ItemsSource="{Binding Properties}" VerticalAlignment="Center" Width="345"
VerticalContentAlignment="Center">
<DataGrid.Resources>
<DataTemplate x:Key="IsSelectedColumnTemplate">
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
<DataTemplate x:Key="IsSelectedColumnTemplateEditing">
<ContentControl x:Name="ControlHolder" Content="{Binding Value}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type System:String}">
<TextBox Text="{Binding ., Mode=TwoWay}" />
</DataTemplate>
<DataTemplate DataType="{x:Type System:Boolean}">
<CheckBox IsChecked="{Binding}"/>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Key" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label VerticalContentAlignment="Center" Height="27" VerticalAlignment="Center" Content="{Binding Key, Mode=OneWay}" Padding="3"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Value" x:Name="myIsSelectedColumn" CellTemplate="{StaticResource IsSelectedColumnTemplate}"
CellEditingTemplate="{StaticResource IsSelectedColumnTemplateEditing}" CanUserSort="True" Width="Auto" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:0)
我有类似的问题。 请看下面的页面:
答案 1 :(得分:0)
我正在用问题解决方案更新我的问题。希望这可以帮助有同样问题的人。
我必须创建一个DataTemplateSelector类,然后在XAML中调用所述类。下面是新的XAML代码以及模板选择器类的副本。
<Window.Resources>
<DataTemplate x:Key="ProviderStringTemplate" DataType="{x:Type System:String}">
<TextBox VerticalAlignment="Center" Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Width="200" />
</DataTemplate>
<DataTemplate x:Key="ProviderBooleanTemplate" DataType="{x:Type System:Boolean}">
<CheckBox Margin="15,0,0,0" VerticalAlignment="Center" IsChecked="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
</DataTemplate>
<Windows:ProviderPropertyTemplateSelector x:Key="templateSelector" />
</Window.Resources>
<DataGrid AutoGenerateColumns="False" Name="dataGridProviderProperties" Height="215" FontSize="11" HorizontalGridLinesBrush="#FFC1C1C1"
VerticalGridLinesBrush="#FFC1C1C1" Padding="0" Background="#00000000" HorizontalScrollBarVisibility="Auto"
CanUserResizeRows="False" AlternationCount="2" AlternatingRowBackground="#05000000" CanUserAddRows="False"
DataContext="{Binding}" ItemsSource="{Binding Properties}" VerticalAlignment="Center" Width="345"
VerticalContentAlignment="Center" IsManipulationEnabled="False" CanUserReorderColumns="False" SelectionUnit="Cell">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Key" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label VerticalContentAlignment="Center" Height="27" VerticalAlignment="Center" Content="{Binding Key}" Padding="3"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Value" CellTemplateSelector="{StaticResource templateSelector}" CanUserSort="True" Width="200" />
</DataGrid.Columns>
</DataGrid>
public class ProviderPropertyTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var property = item as Property<object>;
if (property == null)
return null;
if (property.Value is Boolean)
return ((FrameworkElement)container).FindResource("ProviderBooleanTemplate") as DataTemplate;
if (property.Value is String || property.Value is int)
return ((FrameworkElement)container).FindResource("ProviderStringTemplate") as DataTemplate;
return null;
}
}