2013-02-19 2 views
1

다음은 WPF 스타일이므로 하드 코드 된 열 이름 (이름 및 코드)을 일반화하여 ComboBox에 실제로이 스타일을 적용 할 때 지정할 수있는 방법이 있습니까? 심지어 열 수를 수정할 수도 있습니다. 대신 당신이 당신의 열에 대한 종속성 속성과 사용자 지정 컨트롤을 craeting 고려할 수있는 스타일을 사용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> 

답변

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을 사용자 지정 종속성 속성과 사용자 지정 컨트롤을 추가하고 당신이 당신의 템플릿에 끌어 원하는에 그리드의 이름을 템플릿 함수 적용 여기에서 설정 준비가되었거나 종속성 등록 정보에서 지정한 열을 설정할 수 있습니다.

참고 : 종속성 속성은 실제로는 필요하지 않지만 나중에 필요할 경우 업데이트 콜백의 종속성 속성과 같은 것을 사용하여 약간의 유연성을 구입하는 데 도움이 될 수 있습니다.

+0

그건 좋은 생각 같아. ContentPresenter 계열의 라인에 대해 더 많이 생각하고 있었고, 제 스타일로 넣을 수 있었고 응용 프로그램을 만들 때 할당 할 수있었습니다. 그렇게하기가 쉽지 않은가? – dotNET

+0

쉽게 예,하지만 당신이 물어보고있는 열을 수정하는 유연성을 줄 수 있을지 모르겠습니다. – tam

+0

당신은 ContentPresenter에 아무것도 넣을 수 없다는 것을 의미합니다. 죄송합니다. 저는 WPF 초보자이며 HTML의 div 또는 프레임처럼 ContentPresenter에 대한 인상을 받았습니다. – dotNET