2011-04-20 2 views
6

WPF DataGrid의 개별 셀에서 취소 선 스타일로 글꼴을 설정하는 가장 좋은 방법은 무엇입니까? WPF DataGrid의 개별 셀에 취소 선을 설정하는 가장 좋은 방법은 무엇입니까?

... 나는 각각의 세포에있는 TextBlock 컨트롤을 삽입하거나 DataGridTemplateColumn을 사용하는 알고 있어요

옵션 - 그리고 그 안에 TextDecorations 속성을 사용. 어느 쪽이든 이것은 상당한 임무이며, DataGrid의 기본 AutoGenerate Columns 함수를 사용하고 싶습니다. 특히 ItemsSource가 DataTable이므로.

그렇다면 기본 DataGridTextColumn을 사용하여 생성 된 TextBlock에 액세스 할 수있는 방법이 있습니까?

답변

6
<DataGridTextColumn Binding="{Binding Name}"> 
    <DataGridTextColumn.ElementStyle> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="TextDecorations" Value="Strikethrough"/> 
     </Style> 
    </DataGridTextColumn.ElementStyle> 
</DataGridTextColumn> 

물론 세터를 DataTrigger에 랩핑하여 선택적으로 사용할 수 있습니다.

+1

이 말은 전체 열을하고 있다면, 그것을 할 수있는 좋은 방법입니다, 당신은 좋은이 수행

<Setter Property="TextDecorations" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path =Item.SomeProperty, Converter={StaticResource SomePropertyToTextDecorationsConverter}}" /> 

는 컨버터는 것을 가정하는 boolean 형이며, 다음과 같습니다 개별 세포를하는 방법? 또한 필자는 DataGridTextColumn을 쉽게 바인딩 할 수있는 것이 없으므로 코드에서이 작업을 수행해야합니다. – SDK

+1

내가 말했듯이, DataTrigger를 사용하여 특정 셀로 제한하고, 뒤에서 코드를 사용하는 것은 문제가되지 않아야합니다. 단지 번역의 문제 일뿐입니다. –

0

특정 셀을 기반으로 취소 선을 바인딩하려는 경우 DataGridTextColumn.Binding이 TextBox.Text의 내용 만 변경하기 때문에 바인딩 문제가 발생합니다.

<Setter Property="TextDecorations" 
    Value="{Binding RelativeSource={RelativeSource Self}, 
    Path=Text, 
    Converter={StaticResource TextToTextDecorationsConverter}}" /> 

당신이 TextBox.Text 다른 뭔가에 바인딩 할 경우에, 당신 인 DataGridRow를 통해 바인드 할 필요가 : Text 속성의 값이 모든 경우 당신은 당신이 텍스트 상자 자체에 바인딩 할 수 있습니다 필요 시각적 트리에서 TextBox의 부모. DataGridRow에는 전체 행에 사용 된 전체 개체에 대한 액세스를 제공하는 Item 속성이 있습니다.

public class SomePropertyToTextDecorationsConverter: IValueConverter { 
    public object Convert(object value, Type targetType, object parameter, 
    CultureInfo culture) 
    { 
     if (value is bool) { 
     if ((bool)value) { 
      TextDecorationCollection redStrikthroughTextDecoration = 
      TextDecorations.Strikethrough.CloneCurrentValue(); 
      redStrikthroughTextDecoration[0].Pen = 
      new Pen {Brush=Brushes.Red, Thickness = 3 }; 
      return redStrikthroughTextDecoration; 
     } 
     } 
     return new TextDecorationCollection(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
    } 
관련 문제