Is it possible to have a WPF Toolkit Data Grid's DataGridComboBoxColumn "exposed" when the Data Grid loads? By default you have to click in the cell to expose the combo box. I'd like the user to see that the combo box is available without having to click in the cell. I would prefer that the combo box be immediately available and the first click in the cell makes the combo box actually drop down. Currently you have to click the cell and then click the combo box drop down to expose the values.
V.S.
XAML:
<dg:DataGridComboBoxColumn x:Name="ctrlStatus" Header="Status" Width="Auto" SelectedValueBinding="{Binding Port}" SelectedValuePath="Status">
<dg:DataGridComboBoxColumn.CellStyle>
<Style TargetType="dg:DataGridCell">
<EventSetter Event="Selector.SelectionChanged" Handler="SelectionChanged"/>
</Style>
</dg:DataGridComboBoxColumn.CellStyle>
</dg:DataGridComboBoxColumn>
Code Behind:
List<string> _statusList;
public List<string> StatusList
{
get
{
return _statusList;
}
set
{
_statusList = value;
ctrlStatus.ItemsSource = _statusList;
}
}
Thanks, GAR8
Final SOLUTION: XAML
<telerik:GridViewComboBoxColumn Header="Status">
<telerik:GridViewComboBoxColumn.CellTemplate>
<DataTemplate>
<telerik:RadComboBox ItemsSource="{Binding StatusList,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" SelectedValue="{Binding Port}" SelectedValuePath="Status" SelectionChanged="SelectionChanged"/>
</DataTemplate>
</telerik:GridViewComboBoxColumn.Cell开发者_StackOverflowTemplate>
</telerik:GridViewComboBoxColumn>
Code Behind:
List<string> _statusList;
public List<string> StatusList
{
get { return _statusList; }
set { _statusList = value; }
}
You can use a DataGridTemplateColumn and place a ComboBox as the cell edit template without specifying a non-edit template. This will let the DataGrid use always the ComboBox.
Update
As requested in your comment, below an example. Please note that the example is not optimal and I would have choosen another design, but I have done it in a way so that it should integrate in your solution without bigger problems. I have not tested it. Make a comment if they are errors in.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<ComboBox x:Name="ctrlStatus"
SelectedValueBinding="{Binding Port}"
SelectedValuePath="Status">
SelectionChanged="SelectionChanged"
ItemsSource="{Binding StatusList,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
To use the above code, StatusList must implement change notification. If your DataGrid is not in aWindow but in another class such as in a UserControl, replace the type name in the relative source.
Try this
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource=”{Binding Path=YourSource...}”
Text=”{Binding Path=YourSource...}”/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<ComboBox ItemsSource=”{Binding Path=YourSource...}”
Text=”{Binding Path=YourSource...}”/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Basically you define the ComboBox in both cases which are CellTemplate and CellEditingTemplate.
See this post which I wrote some time ago, in that I wrote separate template for non editing( which you see initially) and editing (which you see when you click i.e combobox) state of cell. Now you can copy the code of editing in non-editing as I have done in XAML above and your problem will be solved
If you need to use this often, then a custom column can be defined:
public class DataGridCustomComboBoxColumn : DataGridComboBoxColumn
{
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
return base.GenerateEditingElement(cell, dataItem);
}
}
This can then be used in place of the normal DataGridComboBoxColumn
.
精彩评论