WPF风格:泛化



作为WPF样式,有没有一种方法可以概括硬编码的列名(Name和Code),这样我就可以在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>

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

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

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

<!-- 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.
     }
}

在Summary中,为您添加具有自定义依赖项属性的自定义控件,然后在您的主题/generic.xaml中放入模板,并在on-apply-template函数中将网格命名为要拉入模板的内容。从那里,您可以准备好设置,也可以设置在dependency属性中指定的列。

注意:dependency属性实际上并不是必需的,但它可以帮助您在以后使用诸如change回调中的dependence属性之类的东西进行更新时获得更大的灵活性。

相关内容

  • 没有找到相关文章

最新更新