2013-02-28 4 views
0

하이라이트 효과를 만들기 위해 텍스트 블록의 스타일을 만들려고합니다. 이 스타일은 각각 다른 속성에 바인딩 된 여러 TextBlock에 사용됩니다. 내가 작업과 같은 바인딩 TextBlock을 하나씩 만들 수 있습니다컨트롤의 Datacontext를 속성으로 설정하십시오.

this.DataContext = dataobject; 

: 내 주요 컨트롤의 데이터 컨텍스트는 뒤에 코드에서 설정

<TextBlock Text="Field1"> 
    <TextBlock.Style> 
     <Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=.}" Value="True"> 
        <Setter Property="Background" Value="Yellow"/> 
        <Setter Property="Foreground" Value="Black"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </TextBlock.Style> 
<TextBlock> 

하지만 바인딩을 변경해야 스타일이 할 수 있도록 다른 TextBlocks에서 사용할 수 있습니다. 뭔가 같은 :이 때

<Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Path=.}" Value="True"> 
      <Setter Property="Background" Value="Yellow"/> 
      <Setter Property="Foreground" Value="Black"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

<TextBlock Text="Field1" DataContext="{Binding Path=BooleanProperty1}" Style="{StaticResource HighlightStyle}"/> 
<TextBlock Text="Field2" DataContext="{Binding Path=BooleanProperty2}" Style="{StaticResource HighlightStyle}"/> 

, 속성을 변경하면 된 본체를 강조하기 위해 아무것도하지 않는다. 이것을 달성 할 수있는 방법이 있습니까?

+0

코드가 누락되었지만 코드의 하단 부분이 완벽하게 작동합니다. 'BooleanProperty1 = true'를 설정하면 노란색 배경이 표시되고 그렇지 않으면 흰색이 나타납니다 ... (다른 TextBlock의 BooleanProperty2와 동일) INPC가 누락 되었습니까? – Blachshma

+0

부울 속성을 어떻게 설정하고 있습니까? 그것들은 dataObject의 일부이며 INPC를 구현합니다. 런타임에 코드를 보면 속성은 true이지만 컨트롤의 DataContext는 false를 표시합니다. –

+0

문제는 텍스트 블록이 콘텐츠 발표자의 내부에 있다는 것이 었습니다. 즉, datacontext를 낮추려면 바인딩이 작동하지 않거나 정적으로 유지되도록 설정하려고했기 때문입니다. 이 의견은 내 문제를 해결하는 데 도움이되었지만 답변으로 표시하는 방법을 모르겠습니다. –

답변

2

태그 속성을 악용 할 수 있습니다.

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Style.Triggers> 
       <Trigger Property="Tag" Value="True"> 
        <Setter Property="Background" Value="Yellow"/> 
        <Setter Property="Foreground" Value="Black"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <TextBlock Text="Field1" Grid.Row="0" Tag="{Binding Field1}"/> 
     <TextBlock Text="Field2" Grid.Row="1" Tag="{Binding Field2}"/>   
    </Grid> 
</Window> 

또는 연결된 종속성 속성을 만들어 전달할 수 있습니다.

public class TextBlockExtender : DependencyObject 
{ 
    public static string GetMyDataField(DependencyObject obj) 
    { 
     return (string)obj.GetValue(MyDataFieldProperty); 
    } 

    public static void SetMyDataField(DependencyObject obj, string value) 
    { 
     obj.SetValue(MyDataFieldProperty, value); 
    } 

    public static readonly DependencyProperty MyDataFieldProperty = 
     DependencyProperty.RegisterAttached("MyDataField", typeof(string), typeof(TextBlockExtender), new PropertyMetadata(null)); 
} 

는 궁극적 인 문제는 내 TextBlock의 사용자 정의 컨트롤에 있었다 콘텐츠 발표자의 내부에 있다는 사실 때문 XAML

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication2" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Style.Triggers> 
       <Trigger Property="local:TextBlockExtender.MyDataField" Value="True"> 
        <Setter Property="Background" Value="Yellow"/> 
        <Setter Property="Foreground" Value="Black"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <TextBlock Text="Field1" Grid.Row="0" local:TextBlockExtender.MyDataField="{Binding Field1}"/> 
     <TextBlock Text="Field2" Grid.Row="1" local:TextBlockExtender.MyDataField="{Binding Field2}"/> 
    </Grid> 
</Window> 
관련 문제