通过样式内的xaml将datagridcomboboxcolumn中组合框的内容居中 [英] Center content in combobox within datagridcomboboxcolumn through xaml within style

查看:104
本文介绍了通过样式内的xaml将datagridcomboboxcolumn中组合框的内容居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题,请保持温柔。
我已经尝试了好几天才能弄清楚。我得到的最接近的结果是以编程方式访问后面代码中的单元格内容。有没有办法在xaml中做到这一点?

my first question, so please be gentle. I have tried for several days to figure it out. The closest I got was to programmatically access the content of the cell in the code behind. Is there a way to do it in the xaml?

例如,这不起作用

<Style x:Key="ComboBox" TargetType="DataGridCell">
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>

<DataTemplate x:Key="foo">
    <Border Margin="3">
        <DataGrid Style="{StaticResource DGStyle}" Block.TextAlignment="Center">
           <DataGrid.Columns>
                <DataGridComboBoxColumn Header="bar" SelectedItemBinding="{Binding ListofIntegers, Mode=TwoWay}" 
                    ItemsSource="{Binding Source={StaticResource fooItem}, Path=ListofBar, Mode=OneWay}" CellStyle="{StaticResource ComboBox}" TextBlock.TextAlignment="Center"/>                    
                </DataGrid.Columns>
        </DataGrid>
    </Border>
</DataTemplate>

我尝试了其他一些操作,能够将标题和文本框居中放置在文本框列中。组合框虽然不想与我合作。有人知道吗?

I have tried a few other things, having been able to center the headers, and textboxes in textbox columns. The comboboxes are not wanting to cooperate with me though. Any one know how?

编辑:好吧,现在觉得很愚蠢,在玩安迪的解决方案,偶然发现了这个

Ok, feel stupid now, was playing around with Andy's solution and stumbled on this

<Style x:Key="BasicComboBox" TargetType="ComboBox">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ComboBoxItem">
                    <!--can mess with appearance of drop down menu here-->
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

<DataGridComboBoxColumn Header="Size" HeaderStyle="{StaticResource Header}"  SelectedItemBinding="{Binding Size.Name, Mode=TwoWay}" 
  ItemsSource="{Binding Source={StaticResource InsertSizes}}" EditingElementStyle="{StaticResource BasicComboBox}"/>

可以在EditingElementStyle中设置生成的组合框的样式。

Can set the style of the generated combobox in EditingElementStyle.

编辑2:尽管上面的方法可行,但我想我会遵循安迪关于使用模板列的建议,组合框也不会与网格融合在一起,但是对于用户而言却更加容易选择,因为不需要打开编辑模式。

Edit 2: While the above works, I think I'm going to go with Andy's suggestion of using a template column, the comboboxes don't blend in with the grid as well, but are much easier for the user to select, as there is no need to open editing mode.

推荐答案

您可以使用模板列代替,此处有一些垃圾数据,但绑定在ComboBox上的工作方式与从DataGridComboBoxColumn进行绑定的方式完全相同。

You could use a template column instead, I've used some junk data here but bindings work on the ComboBox in exactly the same way as they would from your DataGridComboBoxColumn.

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <DataGrid>
        <DataGrid.Items>
            <sys:String>a</sys:String>
        </DataGrid.Items>
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="300">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox HorizontalContentAlignment="Center">
                            <ComboBox.Items>
                                <sys:String>string1</sys:String>
                                <sys:String>string2</sys:String>
                                <sys:String>string3</sys:String>                                
                            </ComboBox.Items>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Window>

这篇关于通过样式内的xaml将datagridcomboboxcolumn中组合框的内容居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆