2

我是一名 iOS 开发人员,现在我正在接近 .net windows 8 商店应用程序开发。好吧,我有这个问题:我用 XAML 创建了一个分组列表视图,如下所示:

<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 predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined 或进口的

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

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

我不确定这意味着什么...

4

2 回答 2

1

The only thing that jumps out at me is that perhaps you should be using Transparent instead of x:Null for your Button brushes.

So, your button markup would become:

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

If this does work, it would be because a button with a Transparent background still accepts a click event, while a button with a null background might not.

于 2013-10-09T19:28:04.403 回答
0

不要这样做。不要从代码中更改 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>

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

于 2013-10-15T09:16:34.340 回答