我有一个WPF Datagrid,我想在其中实现一个CheckCombobox列。我下载了extendedToolkit,如下所示,
<DataGridTemplateColumn Header="Operation" MinWidth="150" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SelectedOperations,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<my:CheckComboBox x:Name="_combo" Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ItemsSource="{Binding Path=AllOperations,UpdateSourceTrigger=Default}"
DisplayMemberPath="OperationName"
ValueMemberPath="OperationName"
SelectedItem="{Binding SelectedOperation, Mode=TwoWay}"
ItemSelectionChanged="_combo_ItemSelectionChanged" SelectedValue="{Binding SelectedOperations, Mode=TwoWay}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
我的问题是,在列失去焦点时,所有选中的值都会消失,因此当下次打开时,我将拥有所有默认值。任何人都可以让我知道我哪里错了... viewModel如下所示(只显示了下面代码中使用的部分)
public ObservableCollection<OperationMasterVM> AllOperations
{
get
{
//SelectedOperation.Clear();
return DAL.GetAllOperations();
}
}
private Dictionary<string,string> _operation = new Dictionary<string,string>();
public Dictionary<string, string> SelectedOperation
{
get
{
return _operation;
}
set
{
_operation = value;
}
}
private string _selectedOperations;
public string SelectedOperations
{
get
{
_selectedOperations = string.Empty;
if (SelectedOperation.Any())
{
foreach (var operation in SelectedOperation)
{
if (string.IsNullOrEmpty(_selectedOperations))
{
_selectedOperations = operation.Value;
}
else
{
_selectedOperations = _selectedOperations + ", " + operation.Value;
}
}
}
return _selectedOperations;
}
set
{
_selectedOperations = value;
}
}
答案 0 :(得分:2)
我将通过向您展示我如何使用此控件来刺激它:
<xctk:CheckComboBox x:Name="cboResults"
KeyDown="CheckComboBox_OnKeyDown"
ItemsSource="{Binding Path=SelectableOptions}"
DisplayMemberPath="Display"
ValueMemberPath="Value"
SelectedMemberPath="IsSelected"/>
其中SelectableOptions是SelectableOption类型的Observable集合:
public class SelectableOption
{
public string Display { get; set; }
public string Value { get; set; }
public bool IsSelected { get; set; }
}
您不必构建所选选项的分隔字符串。这是在您进行选择并折叠下拉列表时创建的。
我还注意到上述评论之一提到了键盘导航。我通过创建一个KeyDown事件处理程序来处理这个问题,这样当CheckComboBox有焦点并且用户按下空格键时,下拉列表将会打开。
private void CheckComboBox_OnKeyDown(object sender, KeyEventArgs e)
{
var obj = (CheckComboBox) sender;
if (e.Key == Key.Space)
{
obj.IsDropDownOpen = !obj.IsDropDownOpen;
}
}
关于此控件的文档有点稀疏。我希望这有点帮助。