【问题标题】:XAML Grouped listView: Changing a row background colorXAML Grouped listView:更改行背景颜色
【发布时间】:2013-10-05 10:34:08
【问题描述】:

我是一名 iOS 开发人员,现在我正在接近 .net windows 8 商店应用程序开发。 好吧,我有这个问题:我用 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 的帮助下,我发现我实际上在这个属性上遇到了问题: ((Grid)((Button)e.OriginalSource).Parent).Background预定义类型'Microsoft.CSharp.RuntimeBinder.Binder'未定义或导入

这是意大利语的英文翻译(我的ide是意大利语)。

当我进入引用->添加引用->程序集->框架时,我只能看到一条消息,说我已经拥有对框架的所有引用,并且我需要使用“对象查看器”来探索框架...

我不知道这意味着什么......

【问题讨论】:

  • 您的帖子被标记为与 WPF 相关,但您的问题表明您正在构建一个 Windows 应用商店应用程序,该应用程序将使用 Windows 运行时而不是 WPF。它是哪一个?尽管它们都是基于 XAML,但还是有一些相当大的差异。
  • Windows 商店应用。对不起

标签: c# .net xaml listview windows-runtime


【解决方案1】:

唯一让我大吃一惊的是,也许您应该使用Transparent 而不是x:Null 作为您的按钮画笔。

因此,您的按钮标记将变为:

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

如果这确实有效,那是因为具有透明背景的按钮仍然接受点击事件,而具有空背景的按钮可能不接受。

【讨论】:

  • 方法 headerClicked 肯定会被调用。我在里面放了一些 Debug.WriteLine
  • 好吧,我想这不是问题所在(不过,使用透明而不是 x:Null 可能是一种好习惯)。如果您在该代码中设置断点,设置后网格的背景是否真的变为蓝色?
  • 没办法。它没有。但是如果我在 Debug 中打印对象 ((Grid)((Button)e.OriginalSource).Parent),我可以看到它实际上是预期的 Grid。我真的无法理解这种行为。
  • 所以你可以毫无例外地设置值,但是当你检查背景的值时,它仍然是原来的颜色(#FFB9B9B9)?
  • 我无法正确检查它。 debug.writeline 说我是 Windows.UI.Xaml.Media.SolidColorBrush
【解决方案2】:

不要这样做。不要从代码中更改 UI 元素。而是对它们进行数据绑定。您可以使用转换器触发按钮。将按钮本身数据绑定到新的布尔属性(已选择),并连接到转换器。

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

网上有很多关于如何使用转换器的文章。

您可以做的另一件事是将按钮的样式设置为资源键。然后,您可以为 GotFocus 和 LostFocus 属性定义触发器。在 gotfocus 上将颜色设置为一种颜色,在 lostfocus 上设置另一种颜色。

这是我让其应用于所有按钮的模板之一。您可以设置特定键,并将按钮绑定到手写笔。

<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>

这样你就不需要做任何事件处理了;)

【讨论】:

  • 要使其成为自定义样式,只需定义 Key 属性而不是 TargetType
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 2016-01-11
  • 2018-05-14
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多