2011-06-22 71 views
0

我是WPF的新手,需要一些幫助。我喜歡使用自定義用戶控件作爲Template而不是在StyleControlTemplate中定義它們,主要是因爲我可以在編寫代碼時看到結果。這種方法在我開始時效果很好,但我不確定如何檢查我的控件上的觸發器並更改UserControl模板中的內容。希望有人能夠理解我在這裏想說的話。反正這裏有一些代碼。我想,如果從ListView [MenuTray]一個項目是一個ListViewItem控制的DataTemplate內選定知道...樣式,數據類型和觸發器

MainWindow.xaml

<Window x:Class="Cellestus.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Width="1000" Height="650" Loaded="Window_Loaded"> 

    <Window.Resources> 
     <ResourceDictionary Source="/Resources/GlobalResources.xaml" /> 
    </Window.Resources> 

    <Viewbox x:Name="WindowView" Stretch="Fill"> 
     <Canvas x:Name="WindowCanvas" Width="1000" Height="650" Background="{StaticResource MainWindow_Bg}"> 
      <!-- other stuf... --> 
      <ListView x:Name="MenuTray" Width="1000" Height="60" Canvas.Bottom="0" Style="{StaticResource MenuTray_Style}" /> 
     </Canvas> 
    </Viewbox> 
</Window> 

MainWindow.xaml.cs,方法:Window_Loded

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    // MenuTrayItems.List: ObservableCollection<MenuTrayItem> 
    // MenuTrayItem: Class with a couple of string variables, Title, image, page and etc.. 
    MenuTray.ItemsSource = MenuTrayItems.List; 
} 

/Resources/GlobalResources.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:class="clr-namespace:Cellestus.Classes" 
        xmlns:view="clr-namespace:Cellestus.Views"> 

    <!-- Brushes --> 
    <ImageBrush x:Key="MainWindow_Bg" ImageSource="/Images/Backgrounds/MainWindow.png" Stretch="Fill" /> 

    <LinearGradientBrush x:Key="Gradient_Black" StartPoint="0.5,0" EndPoint="0.5,1"> 
     <GradientStop Color="#2c2c2c" Offset="0" /> 
     <GradientStop Color="#151515" Offset="1" /> 
    </LinearGradientBrush> 

    <LinearGradientBrush x:Key="Gradient_LightGray" EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="#6B6B6B" Offset="1" /> 
     <GradientStop Color="#D6D7D8" Offset="0" /> 
    </LinearGradientBrush> 

    <LinearGradientBrush x:Key="Gradient_Gray" StartPoint="0.5,0" EndPoint="0.5,1"> 
     <GradientStop Color="#515151" Offset="0" /> 
     <GradientStop Color="#282828" Offset="1" /> 
    </LinearGradientBrush> 

    <LinearGradientBrush x:Key="Gradient_Green" StartPoint="0.5,0" EndPoint="0.5,1"> 
     <GradientStop Color="#77ba53" Offset="0" /> 
     <GradientStop Color="#5a9c37" Offset="1" /> 
    </LinearGradientBrush> 

    <!-- Templates --> 
    <ItemsPanelTemplate x:Key="HorizontalListView"> 
     <StackPanel Orientation="Horizontal" /> 
    </ItemsPanelTemplate> 

    <DataTemplate x:Key="MenuTrayItem_Template" DataType="{x:Type class:MenuTrayItem}"> 
     <view:MenuTrayItemView /> 
    </DataTemplate> 

    <!-- Styles --> 
    <Style x:Key="MenuTray_Style" TargetType="{x:Type ListView}"> 
     <Setter Property="BorderThickness" Value="0" /> 
     <Setter Property="Background" Value="{StaticResource Gradient_Black}" /> 
     <Setter Property="ItemsPanel" Value="{StaticResource HorizontalListView}" /> 
     <Setter Property="ItemTemplate" Value="{StaticResource MenuTrayItem_Template}" /> 
    </Style> 

</ResourceDictionary> 

/Views/MenuTrayItemView.xaml

<UserControl x:Class="Cellestus.Views.MenuTrayItemView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d"> 

    <UserControl.Resources> 
     <ResourceDictionary Source="/Resources/GlobalResources.xaml" /> 
    </UserControl.Resources> 

    <Border x:Name="ItemContainer" Width="150" Height="60" CornerRadius="10" Background="{StaticResource Gradient_Gray}" MouseEnter="ItemContainer_MouseEnter" MouseLeave="ItemContainer_MouseLeave"> 
     <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
      <Image Margin="0,3,0,0" Width="60" Height="30" Source="{Binding Image, FallbackValue='/Images/Icons/MenuTray/default.png'}" /> 
      <TextBlock Margin="3,0,0,0" Text="{Binding Title, FallbackValue='title'}" FontSize="10" Foreground="White" /> 
     </StackPanel> 
    </Border> 
</UserControl> 

/Views/MenuTrayItemView.xaml.cs

private void ItemContainer_MouseEnter(object sender, RoutedEventArgs e) 
{ 
    ItemContainer.Background = FindResource("Gradient_LightGray") as Brush; 
} 

private void ItemContainer_MouseLeave(object sender, RoutedEventArgs e) 
{ 
    ItemContainer.Background = FindResource("Gradient_Gray") as Brush; 
} 

下面是視覺的人的形象: image

那麼,如果你做到了這一點。我想知道的是如何在/UserControl(MenuTrayView.xaml)中的ListViewItem控件上捕獲IsSelected值,並將背景更改爲靜態資源「Gradient_Green」。我知道我可以在我看來處理MouseDown事件,但我不想這樣做。我想知道該物品是否真的被選中。

回答

0

你需要創建一個樣式到您的用戶控件,這樣做

<Style TargetType="{x:Type local:MenuTrayItemView}"> 
    <Trigger Property="IsSelected" Value="true"> 
     <Setter Property="background" Value="Static Resource Gradient_Green"/> 
    </Trigger> 
    </Style.Triggers> 
</Style> 

如果你OT問題去這裏WPF Trigger for IsSelected in a DataTemplate for ListBox items

+0

kinnrot嘿,這並沒有爲我工作,我得到了錯誤MenuTrayItemView沒有名爲「IsSelected」的屬性。我正在嘗試關注你發給我的鏈接,但我再次陷入困境。 –