2013-10-05 37 views
2

我是一名iOS開發者,現在我接近.NET的Windows 8商店應用的開發現在。 嗯,我有這樣的問題:我創建了一個分組的ListView使用XAML這樣的:XAML分組的ListView:更改行背景顏色

<ListView Margin="0,10,50,50" 
      x:Name="listView" 
      AutomationProperties.AutomationId="listView" 
      AutomationProperties.Name="Grouped Items" 
      Grid.Row="1" 
      Background="LightGray" 
      BorderBrush="#FF818181" 
      ItemsSource="{Binding Source={StaticResource groupedIndexViewSource}}" 
      ItemTemplate="{StaticResource StandardSubIndexNOIcon320x70ItemTemplate}" 
      BorderThickness="0,1,1,1" 
      SelectionMode="None" 
      IsSwipeEnabled="false" 
      IsItemClickEnabled="True" 
      ItemClick="itemClicked"> 
    <ListView.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.HeaderTemplate> 
     <DataTemplate> 
      <Grid x:Name="Prr" 
       Width="315" 
       Margin="-5,0,0,2" 
       Height="70" 
       Background="#FFB9B9B9"> 
      <Button Width="315" 
        Height="70" 
        Margin="0" 
        Click="HeaderClicked" 
        BorderThickness="0" 
        BorderBrush="{x:Null}" 
        Foreground="{x:Null}" 
        Background="{x:Null}" 
        Padding="0"> 
       <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="260" /> 
        <ColumnDefinition Width="50" /> 
       </Grid.ColumnDefinitions> 

       <TextBlock Grid.Column="0" 
          Width="250" 
          Margin="10,0,0,0" 
          Text="{Binding Title}" 
          Style="{StaticResource BodyTextStyle}" 
          TextWrapping="NoWrap" 
          FontSize="20" 
          Foreground="#DE000000" 
          VerticalAlignment="Center" 
          TextAlignment="Left" 
          HorizontalAlignment="Left" /> 
       </Grid> 
      </Button> 
      </Grid> 
     </DataTemplate> 
     </GroupStyle.HeaderTemplate> 
    </GroupStyle> 
    </ListView.GroupStyle> 
</ListView> 

現在,我想改變標題按鈕的背景顏色在HeaderTemplate中單擊了定義,因此,在我的c#代碼我已經定義了這個方法:

private void HeaderClicked(object sender, RoutedEventArgs e) 
    { 
     ((Grid)((Button)e.OriginalSource).Parent).Background = new SolidColorBrush(Colors.DarkBlue); 
} 

問題是這條指令被忽略。

你能幫我解決這個問題嗎?

在此先感謝。

更新:

隨着DanM我剛剛發現,我其實對這個屬性問題的幫助: ((網格)((按鈕)e.OriginalSource).Parent)裝置技術領域predefinite型「微軟.CSharp.RuntimeBinder.Binder」沒有定義或導入

這是從意大利翻譯成英文(我的IDE是意大利人)。

當我在引用 - >添加引用 - >程序集 - >框架我只能看到一條消息,說我已經所有引用框架,並且我需要使用「對象查看器」來探索引用框架...

我不知道這意味着什麼......

+0

您的帖子被標記爲與WPF,但你的問題建議你正在建設一個Windows Store應用程序,這將使用Windows Runtime和不是WPF。這是什麼?儘管它們都基於XAML,但還是有一些相當大的差異。 –

+0

Windows商店應用程序。對不起 –

回答

1

,在跳出我的唯一的事情是,也許你應該使用Transparent,而不是x:Null您的按鈕刷子。

所以,你的按鈕標記將成爲:

<Button Width="315" 
     Height="70" 
     Margin="0" 
     Click="HeaderClicked" 
     BorderThickness="0" 
     BorderBrush="Transparent" 
     Foreground="Transparent" 
     Background="Transparent" 
     Padding="0"> 

如果這樣做的工作,那將是因爲有一個透明背景的按鈕仍然接受click事件,同時用空背景的按鈕可能不會。

+0

肯定會調用headerClicked方法。我把一些Debug.WriteLine放在 –

+0

裏面,好吧,我想這不是問題(可能是使用Transparent而不是x:Null的好習慣)。如果您在該代碼中放置斷點,則在設置網格後,網格的背景會變爲藍色嗎? – devuxer

+0

沒辦法。它沒有。但如果我打印調試對象((網格)((按鈕)e.OriginalSource).Parent)我可以看到,它實際上是一個預期的網格。我真的無法理解這種行爲。 –

0

不這樣做。不要從代碼中更改UI元素。而是數據綁定它們。您可以使用轉換器在按鈕上旋轉。將按鈕本身綁定到新的布爾屬性(Selected),並連接到轉換器。

<Button BackGroud="{Bind Selected, Converter={StaticResource selectedToBrushConverter}}" /> 

有很多文章如何在互聯網上使用轉換器。

你可以做的另一件事是按鈕的樣式設置爲一個資源鍵。然後,您可以將觸發器定義到GotFocus和LostFocus屬性。在gotfocus上將顏色設置爲一種顏色,將焦點設置爲另一種顏色。

這是我讓我適用於所有按鈕的模板之一。您可以設置特定的按鍵,並將按鈕綁定到styla。

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Margin" Value ="1" /> 
    <Setter Property="Background" Value="Gray" /> 
    <Setter Property="OverridesDefaultStyle" Value="True" /> 
    <Setter Property="Foreground" Value="White" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="BorderBrush" Value="White" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}" > 
       <Border Background="{TemplateBinding Background}" Opacity="{TemplateBinding Opacity}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3"> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="3"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="DarkOrange" /> 
     </Trigger> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter Property="Background" Value="White" /> 
      <Setter Property="Foreground" Value="Gray" /> 
      <Setter Property="Opacity" Value="0.95" /> 
      <!--<Setter Property="BorderThickness" Value="2" />--> 
     </Trigger> 
     <Trigger Property="IsFocused" Value="True"> 
      <Setter Property="Background" Value="#82A3FF" /> 
     </Trigger> 

     <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Background" Value="DarkGray" /> 
     </Trigger> 
    </Style.Triggers> 

</Style> 

這種方式,你有沒有必要做任何eventhandling;)

+0

要使其成爲自定義樣式,只需定義Key屬性而不是TargetType – user853710