如何让 WPF DataGrid 和 AutoCompleteBox 的选定项发挥良好作用?
我在 WPF 表单上使用 AutoCompleteBox 没有任何问题。现在我想在 WPF DataGrid 中做同样的事情。除了 SelectedItem 的 setter 之外,几乎所有内容都有效。我看到 getter 被调用,但在输入值并点击选项卡(或使用箭头键)后,setter 永远不会被调用。在控制台输出中,我没有看到任何绑定错误。我希望有人能告诉我我做错了什么,以及如何让 SelectedItem 在 DataGrid 内时触发 ViewModel 类中属性的设置器。首先是 ViewModel 类的片段:
public static List<ImpaSimple> AllImpas { get { return ImpaListRepository.ImpaList; } }
private ImpaSimple _selectedImpa;
public ImpaSimple SelectedImpa
{
get { return _selectedImpa; }
set
{
if (value == _selectedImpa) return;
_selectedImpa = value;
//Manually set Description and Unit fields because user can override the IMPA default values.
// Description = _selectedImpa.Name;
//TODO Set Units too
RaisePropertyChanged("SelectedImpa");
}
}
XAML
在下面的 XAML 中,我添加了 LostFocus 处理程序背后的代码作为临时解决方法。添加 UpdateSourceTrigger 属性也是为了实现此目的。
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<TextBlock Style="{StaticResource DataGridHeader}">LImpa</TextBlock>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Hots:AutoCompleteBoxEx ToolTip="Start typing an IMPA number"
ItemsSource="{Binding AllImpas}"
Width="50"
HorizontalContentAlignment="Left"
FilterMode="StartsWith"
IsDropDownOpen="True"
IsTextCompletionEnabled="True"
LostFocus="ImpaBoxExLostFocus"
SelectedItem="{Binding SelectedImpa,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Hots:AutoCompleteBoxEx.ItemTemplate>
<DataTemplate>
<Grid Width="450">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="275" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ImpaId}"
Grid.Column="0" />
<TextBlock Text="{Binding Name}"
Grid.Column="1" />
<TextBlock Text="{Binding Unit}"
Grid.Column="2" />
</Grid>
</DataTemplate>
</Hots:AutoCompleteBoxEx.ItemTemplate>
</Hots:AutoCompleteBoxEx>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I've used the AutoCompleteBox without problem on a WPF form. Now I would like to do the same thing inside a WPF DataGrid. Almost everything works except the setter for SelectedItem. I see the getter get called but after typing a value and hitting tab (or using the arrow keys) the setter never gets called. In the console output I see no binding errors. I'm hoping someone can tell me what I'm doing wrong and how to get SelectedItem to fire the setter on the property in ViewModel class when it's inside a DataGrid. First the snippet of the ViewModel class:
public static List<ImpaSimple> AllImpas { get { return ImpaListRepository.ImpaList; } }
private ImpaSimple _selectedImpa;
public ImpaSimple SelectedImpa
{
get { return _selectedImpa; }
set
{
if (value == _selectedImpa) return;
_selectedImpa = value;
//Manually set Description and Unit fields because user can override the IMPA default values.
// Description = _selectedImpa.Name;
//TODO Set Units too
RaisePropertyChanged("SelectedImpa");
}
}
The XAML
In the XAML below I have added a code behind handler for LostFocus as a temporary work-around. The addition of the UpdateSourceTrigger attribute was also an attempt to get this working.
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<TextBlock Style="{StaticResource DataGridHeader}">LImpa</TextBlock>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Hots:AutoCompleteBoxEx ToolTip="Start typing an IMPA number"
ItemsSource="{Binding AllImpas}"
Width="50"
HorizontalContentAlignment="Left"
FilterMode="StartsWith"
IsDropDownOpen="True"
IsTextCompletionEnabled="True"
LostFocus="ImpaBoxExLostFocus"
SelectedItem="{Binding SelectedImpa,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Hots:AutoCompleteBoxEx.ItemTemplate>
<DataTemplate>
<Grid Width="450">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="275" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ImpaId}"
Grid.Column="0" />
<TextBlock Text="{Binding Name}"
Grid.Column="1" />
<TextBlock Text="{Binding Unit}"
Grid.Column="2" />
</Grid>
</DataTemplate>
</Hots:AutoCompleteBoxEx.ItemTemplate>
</Hots:AutoCompleteBoxEx>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,我想我知道那是什么 - 我称之为“害羞的数据上下文”的缺陷 - 尝试设置你的
Hots:AutoCompleteBoxEx DataContext to:
检查它的方法是创建一个虚拟转换器并像这样使用它:
然后在其 Convert 内放置一个断点并检查值。由于没有指定 Path - 输入值是 DataContext 本身,如果它为 null,则它永远不会被设置/丢失。
Ah, I think I know what that is - the defect I call 'shy datacontext' - try setting your
Hots:AutoCompleteBoxEx DataContext to:
The way to check it is create a dummy converter and use it like that:
then put a breakpoint inside its Convert and check for the value. Since no Path is specified - the input value is the DataContext itself, if it's null, then it never gets set/got lost.