简体   繁体   English

WPF Datagrid —具有多行功能的可编辑组合框?

[英]WPF Datagrid — editable combobox with multi-line capability?

In my grid I have a column which needs to provide a basic set of dropdown options, but which the user can manually edit. 在我的网格中,我有一列需要提供一组基本的下拉选项,但用户可以手动编辑。 I've got this part working just fine: 我已经将这部分工作正常了:

            <DataGridTemplateColumn Header="Comment" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding DefaultComments}" Text="{Binding Comment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" VerticalContentAlignment="Center" IsEditable="True" Name="CommentComboBox" Loaded="CommentComboBox_Loaded" Height="50"></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

The additional feature I'd like to provide is to allow multi-line text entry where the user can force a line feed using Ctrl-Enter. 我想提供的其他功能是允许多行文本输入,用户可以在其中使用Ctrl-Enter强制换行。

Is it possible to extend the existing column to do this by perhaps manipulating the TextBox portion of the ComboBox? 是否有可能通过操纵ComboBox的TextBox部分来扩展现有的列来做到这一点? Or will it require a completely different template column? 还是需要一个完全不同的模板列?

Try This below will allow you to add multiline input in textbox inside a combobox. 尝试以下操作可让您在组合框内的文本框中添加多行输入。

Add the style to the Resources 将样式添加到资源

This will work for Enter rather than using Ctrl-Enter. 这将适用于Enter,而不是使用Ctrl-Enter。

 <Window.DataContext>
    <local:MyComboVM/>
</Window.DataContext>
<Window.Resources>
    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
        <Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">                        
                    <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Text, BindsDirectlyToSource=True}"></TextBlock>
                            <TextBox TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
                        </StackPanel>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsHighlighted" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <ComboBox ItemsSource="{Binding MyCombo}"/>
</Grid>

VM VM

public class MyComboVM
{
    public ObservableCollection<Texts> MyCombo { get; set; }        
    public MyComboVM()
    {
        MyCombo = new ObservableCollection<Texts>();
        MyCombo.Add(new Texts() { Text = "This is one" });
        MyCombo.Add(new Texts() { Text = "This is Two" });
    }        
}

public class Texts
{
    public string Text { get; set; }
}

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

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