2009-09-14 38 views
19

Mã này làm việc (khi ControlType = "thả xuống", sau đó nền vàng):Làm cách nào để chuyển đổi chế độ hiển thị của TextBlock trong DataTrigger?

<Window x:Class="TestCollapsed.Views.MainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:c="clr-namespace:TestCollapsed.Commands" 
    Title="Main Window" Height="400" Width="800"> 
    <Window.Resources> 
     <Style x:Key="DropDownStyle" TargetType="TextBlock"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ControlType}" Value="dropDown"> 
        <Setter Property="Background" Value="Yellow"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

    <StackPanel> 
     <TextBlock Visibility="Visible" 
        Text="This is going to be the dropdown control." 
        Style="{StaticResource DropDownStyle}"/> 
    </StackPanel> 
</Window> 

Nhưng mã này không không làm việc (khi ControlType = "thả xuống", sau đó TextBlock vẫn là vô hình):

<Window x:Class="TestCollapsed.Views.MainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:c="clr-namespace:TestCollapsed.Commands" 
    Title="Main Window" Height="400" Width="800"> 
    <Window.Resources> 
     <Style x:Key="DropDownStyle" TargetType="TextBlock"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ControlType}" Value="dropDown"> 
        <Setter Property="Visibility" Value="Visible"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

    <StackPanel> 
     <TextBlock Visibility="Collapsed" 
        Text="This is going to be the dropdown control." 
        Style="{StaticResource DropDownStyle}"/> 
    </StackPanel> 
</Window> 

tại sao tôi không thể thiết lập tầm nhìn trong một phong cách như tôi có thể nền?

Trả lời

42

Bạn đang đặt Chế độ hiển thị trên TextBlock và sau đó cố gắng ghi đè nó bằng một kiểu. Điều đó sẽ không hoạt động. Hãy thử điều này:

<Window x:Class="TestCollapsed.Views.MainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:c="clr-namespace:TestCollapsed.Commands" 
    Title="Main Window" Height="400" Width="800"> 
    <Window.Resources> 
     <Style x:Key="DropDownStyle" TargetType="TextBlock"> 
      <Setter Property="Visibility" Value="Collapsed"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ControlType}" Value="dropDown"> 
        <Setter Property="Visibility" Value="Visible"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

    <StackPanel> 
     <TextBlock Text="This is going to be the dropdown control." 
        Style="{StaticResource DropDownStyle}"/> 
    </StackPanel> 
</Window> 
+10

tôi bị buộc phải sai lầm này 10 lần trước khi nó chìm trong –

+1

Yeah, tôi quá.. Bây giờ, đây là điều đầu tiên tôi tìm kiếm khi xem xét mã bằng Trình kích hoạt. –

+1

chính xác là vấn đề, cảm ơn –

-1

Tôi có cùng một vấn đề. @ Bryan của câu trả lời là hoàn hảo! Có các phiên bản sai và đúng. Phiên bản sai:

<TextBlock Text="1999-09-09 16:08" VerticalAlignment="Top" Visibility="Collapsed"> 
           <TextBlock.Style> 
            <Style BasedOn="{StaticResource TipTextYellow}" TargetType="TextBlock"> 
             <Style.Triggers> 
              <DataTrigger Binding="{Binding ElementName=Alcohol,Path=IsFocused}" Value="True"> 
               <Setter Property="Visibility" Value="Visible"/> 
              </DataTrigger> 
             </Style.Triggers> 
            </Style> 
           </TextBlock.Style> 
          </TextBlock> 

Phiên bản đúng:

<TextBlock Text="1999-09-09 16:08" VerticalAlignment="Top"> 
           <TextBlock.Style> 
            <Style BasedOn="{StaticResource TipTextYellow}" TargetType="TextBlock"> 
             <Setter Property="Visibility" Value="Collapsed"/> 
             <Style.Triggers> 
              <DataTrigger Binding="{Binding ElementName=Alcohol,Path=IsFocused}" Value="True"> 
               <Setter Property="Visibility" Value="Visible"/> 
              </DataTrigger> 
             </Style.Triggers> 
            </Style> 
           </TextBlock.Style> 
          </TextBlock> 
Các vấn đề liên quan