WPF风格:归纳

WPF风格:归纳

问题描述:

后遭WPF的风格,有没有办法来概括硬编码列名(名称和代码),这样我就可以指定它们在一个ComboBox实际应用这种风格是什么时候?更好的是,如果我甚至可以修改列数?WPF风格:归纳

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

而不是使用一种风格,你可以考虑使用依赖项属性为你的列创建自定义控件。

参与,但它会更好地满足您的需求,特别是具有如果你想重新使用它建立的一点点。

一个例子是像下面这样。其中一些是你应该能够填写的伪代码。

<!-- 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在你的模板下降,并命名电网要拉进在您的模板是什么应用模板功能。从那里开始,您可以准备设置或设置您在依赖项属性中指定的列。

注:依赖属性实际上不是必要的,但它可以帮助以后给你买一点点灵活性上使用的东西像更改回调的依赖特性,如果需要更新。

+0

这似乎是一个不错的主意。我正在考虑更多关于ContentPresenter的东西,我可以将它放在我的样式中,然后在应用程序时分配任何东西。这不容易吗? – dotNET 2013-02-19 05:55:38

+0

更容易,但我不确定它会给你修改你所问的列的灵活性。 – tam 2013-02-20 03:35:16

+0

你的意思是我不能在ContentPresenter中放入任何东西。对不起,我是WPF新手,并且拥有ContentPresenter的印象,就像HTML中的div或frame一样。 – dotNET 2013-02-20 04:11:07