【问题标题】:WPF Datagrid -- editable combobox with multi-line capability?WPF Datagrid - 具有多行功能的可编辑组合框?
【发布时间】:2015-10-07 14:31:19
【问题描述】:

在我的网格中,我有一列需要提供一组基本的下拉选项,但用户可以手动编辑。我的这部分工作得很好:

            <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>

我想提供的附加功能是允许多行文本输入,用户可以使用 Ctrl-Enter 强制换行。

是否可以通过操作 ComboBox 的 TextBox 部分来扩展现有列以执行此操作?还是需要完全不同的模板列?

【问题讨论】:

    标签: c# wpf datagrid combobox multiline


    【解决方案1】:

    试试下面这将允许您在组合框内的文本框中添加多行输入。

    将样式添加到资源中

    这将适用于 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>
    

    虚拟机

    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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-03
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多