2010-07-04 3 views
1

많은 열이있는 DataGrid가 있는데 사용자에게 볼 수있는 열을 선택할 수있는 드롭 다운을 제공하고 싶습니다. .Net 4 WPF DataGrid를 데스크톱 응용 프로그램에서 사용하고 있습니다.열 표시 여부 켜기 및 끄기

누구든지 내가하려는 것을 쉽게 달성 할 수있는 방법을 알고 있습니다.

답변

1

다음과 같이합니다.

DataGridColumn (내가 숨기거나 표시하려는 속성)을 매개 변수로 사용하고 표시되어있는 경우 열을 숨기고 Hide가 아닌 경우이를 표시하는 HideShowColumnCommand라는 ICommand를 추가합니다.

그때 나는 열 표시/숨김 상태를 표시하는 눈금이있는 열 머리글에 연결 까다로운 상황에 맞는 메뉴 ..

상황에 맞는 메뉴가 너무

<ContextMenu 
    ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Column.DataGridOwner.Columns}"> 
    <ContextMenu.Resources> 
     <local:DataGridHeaderVisibilityToBooleanConverter 
     x:Key="visibilityConverter" /> 
     <BooleanToVisibilityConverter 
     x:Key="VisibilityOfBool" /> 

     <DataTemplate 
     DataType="{x:Type DataGridColumn}"> 
     <ContentPresenter 
      Content="{Binding Path=Header}" 
      RecognizesAccessKey="True" /> 
     </DataTemplate> 

    </ContextMenu.Resources> 
    <ContextMenu.ItemContainerStyle> 
     <Style 
     TargetType="MenuItem"> 
     <!--Warning dont change the order of the following two setters 
        otherwise the command parameter gets set after the command fires, 
        not much use eh?--> 
     <Setter 
      Property="CommandParameter" 
      Value="{Binding Path=.}" /> 
     <Setter 
      Property="Command" 
      Value="{Binding Path=DataGridOwner.HideShowColumnCommand}" /> 
     <Setter 
      Property="IsChecked" 
      Value="{Binding Path=Visibility, Converter={StaticResource visibilityConverter}}" /> 
     </Style> 
    </ContextMenu.ItemContainerStyle> 
</ContextMenu> 

등 모양을 사용하여 이런 변환기는

public class DataGridHeaderVisibilityToBooleanConverter :IValueConverter{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
    try { 

     Visibility visibility = (Visibility)value; 
     if (visibility == Visibility.Visible) { 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
    catch { } 
    return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
    throw new NotImplementedException(); 
    } 

    #endregion 
} 
관련 문제