1

在成为 WPF 样式之后,有没有办法概括硬编码的列名(名称和代码),以便在 ComboBox 上实际应用此样式时可以指定它们?更好的是,如果我什至可以修改列数?

 <Style TargetType="ComboBox" x:Key="MultiColumnComboBoxStyle">
        <Style.Resources>
            <Style TargetType="ComboBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <Border>
                                <Grid HorizontalAlignment="Stretch" TextElement.FontWeight="Normal">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="Code" />
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Grid.Column="0" Text="{Binding Path=Name}" />
                                    <Rectangle Grid.Column="1" Width="1" Fill="Black" />
                                    <TextBlock Grid.Column="2" Text="{Binding Path=Code}" Margin="5,0,5,0" />
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel Grid.IsSharedSizeScope="True" IsItemsHost="True" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
4

1 回答 1

0

而不是使用样式,您可以考虑为您的列创建一个具有依赖属性的自定义控件。

涉及一些设置,但它会更好地满足您的需求,特别是如果您想重用它。

一个示例如下所示。其中一些是您应该能够填写的伪代码。

<!-- In your generic.xaml file -->
<Style TargetType="MyCustomComboBox" >
  <Setter Property="Template" >
     <Setter.Value>
        <ControlTemplate TargetType="MyCustomComboBox" >
            <!-- your template code goes here -->
            <Grid x:Name="_myCustomGrid />
         </ControlTemplate> 
      </Setter.Value>
  </Setter>
</Style>

 //then in a cs file inherit from the combo box
public class MyCustomColumnComboBox : ComboBox //get all the combobox functionality
{
     public IList ComboColumns 
     { 
          get { return (IList)GetValue(ComboColumnsProperty);} 
          set { SetValue(ComboColumnsProperty,value);} 
     }
     public static readonly DependencyProperty ComboColumnsProperty = DependencyProperty.RegisterProperty(...);

     private Grid _grid;

     public override OnApplyTemplate() 
     {
         //pull your template grid info here, then use that when setting the columns.
         _grid = GetTemplateChild("_myCustomGrid") as Grid;
         //from here you can check to see if you have your list yet, 
         //if you don't then you maintain the grid for when you do have your list.
         // This can behave different depending on if you are in wpf or silverlight,
         // and depending on how you were to add the items to the dependency property.
     }
}

总之,为您添加具有自定义依赖项属性的自定义控件,然后在您的主题/generic.xaml 中放入您的模板并将网格命名为您想要在应用模板功能中拉入模板的内容。从那里你要么准备好设置,要么可以设置你在依赖属性中指定的列。

注意:依赖属性实际上并不是必需的,但它可以帮助您在以后使用更改回调上的依赖属性之类的东西时获得更多的灵活性,以便在必要时进行更新。

于 2013-02-19T05:47:20.633 回答