简体   繁体   English

不保留在WPF中的扩展工具包值

[英]Extended toolkit values not retaining in WPF

I have a WPF Datagrid and i want to implement a CheckCombobox column in it. 我有一个WPF Datagrid,我想在其中实现一个CheckCombobox列。 I downloaded the extendedToolkit and did as below, 我下载了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>

My problem is, on lost focus of the column all the checked value disappears thus when the next time it opens i have all the default values. 我的问题是,在列失去焦点时,所有检查的值都会消失,因此当下次打开时,我将拥有所有默认值。 Can anyone please let me know where i am going wrong ...The viewModel is shown below (only have shown the part which is used in the below code) 任何人都可以让我知道我哪里错了... 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;
    }
}

I'm going to take a stab at this by showing you how I use this control: 我将向你展示如何使用这个控件:

<xctk:CheckComboBox x:Name="cboResults" 
                            KeyDown="CheckComboBox_OnKeyDown"
                            ItemsSource="{Binding Path=SelectableOptions}"
                            DisplayMemberPath="Display"
                            ValueMemberPath="Value"
                            SelectedMemberPath="IsSelected"/>

Where SelectableOptions is an Observable collection of type SelectableOption: SelectableOptions是SelectableOption类型的Observable集合:

public class SelectableOption
{
    public string Display { get; set; }
    public string Value { get; set; }
    public bool IsSelected { get; set; }
}

You don't have to build the delimited string of selected options. 您不必构建所选选项的分隔字符串。 This is created as you make selections and collapse the dropdown. 这是在您进行选择并折叠下拉列表时创建的。

I also noted that one of the above comments mentioned keyboard navigation. 我还注意到上面的评论之一提到了键盘导航。 I handled this by creating a KeyDown event handler so that when CheckComboBox has focus and the user presses the spacebar, the dropdown will open. 我通过创建一个KeyDown事件处理程序来处理这个问题,这样当CheckComboBox具有焦点并且用户按下空格键时,下拉列表将打开。

private void CheckComboBox_OnKeyDown(object sender, KeyEventArgs e)
{
     var obj = (CheckComboBox) sender;
     if (e.Key == Key.Space)
     {
         obj.IsDropDownOpen = !obj.IsDropDownOpen;
     }
}

The documentation on this control is kind of sparse. 关于此控件的文档有点稀疏。 I hope this helps a little. 希望这会有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM