2011-05-10 43 views
6

我有一個控制標籤..和布爾依賴項屬性「IsLink」...所以,如果IsLink = true,我需要使藍色前景和光標爲「手」以及..XAML中的觸發器

我可以綁定自己創造,但在這種情況下,我需要寫兩個轉換器(BoolToCursor和BoolToForeground),但我懶得爲:)

所以,我tryed水木清華這樣的:

<Label Name="lblContent" VerticalAlignment="Center" FontSize="14"> 
    <Label.Style> 
     <Style TargetType="Label"> 
      <Style.Triggers> 
       <Trigger SourceName="myControl" Property="IsLink" Value="True"> 
        <!--Set properties here--> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Label.Style> 
    label's text 
</Label> 

但它不工作...任何想法,先生們? :)

回答

8

使用DataTrigger,而不是普通Trigger.Check以下

代碼XAML

<Window x:Class="WpfApplication1.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Title="MainWindow" Height="350" Width="525"> 
     <Grid> 
      <Label Name="lblContent" VerticalAlignment="Center" FontSize="14"> 
       <Label.Style> 
        <Style TargetType="Label"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding Path=IsLink}" 
                  Value="True"> 
           <Setter Property="Foreground" Value="Blue" /> 
           <Setter Property="Cursor" Value="Hand" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Label.Style> 
       label's text 
      </Label> 

     </Grid> 
    </Window> 

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = this; 
     } 


     public Boolean IsLink 
     { 
      get { return (Boolean)GetValue(IsLinkProperty); } 
      set { SetValue(IsLinkProperty, value); } 
     } 


     public static readonly DependencyProperty IsLinkProperty = 
      DependencyProperty.Register("IsLink", typeof(Boolean), 
      typeof(MainWindow), new UIPropertyMetadata(false)); 


    } 
+0

非常感謝!但還有一個類似的問題,如果我有兩個標籤,並且如果其他內容爲空,我想隱藏一個標籤,我已經嘗試過這種方式,但它不起作用 – 2011-05-14 13:35:25

2
<CheckBox x:Name="IsLink">IsLink</CheckBox> 
<Label Name="lblContent" 
     VerticalAlignment="Center" 
     FontSize="14"> 
    <Label.Style> 
     <Style> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ElementName=IsLink, Path=IsChecked}" 
           Value="true"> 

        <Setter Property="Label.Foreground" 
          Value="Blue" /> 
        <Setter Property="Label.Cursor" 
          Value="Hand" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Label.Style> 
    label's text 
</Label> 
相關問題