【问题标题】:Could someone give me some adviceabout WPF?有人可以给我一些关于 WPF 的建议吗?
【发布时间】:2013-04-26 10:17:17
【问题描述】:

我正在制作一个数据绑定的用户控件。查询结果包括对象集合 (A),其中 A 具有其他结果 (B) 的集合。所以A包含多个B。

在用户控件中,我想将集合 A 表示为扩展器,将 B 表示为扩展器内的按钮。这就是我得到的

<UserControl x:Class="GuideLib.ModuleControls.uclQuestions"
         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" 
         d:DesignHeight="300" d:DesignWidth="300">
<ItemsControl x:Name="ictlAnswers" ItemsSource="{Binding}" Background="Gray">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander ExpandDirection="Down" Background="DarkGray">
                <Expander.Header>
                    <TextBlock Text="{Binding Path=Name}" Foreground="White"/>
                </Expander.Header>
                <ListBox x:Name="SubListBox" ItemsSource="{Binding Path=Question}" Background="Gray" Foreground="White" HorizontalAlignment="Stretch">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                                <Button Content="{Binding Path=Name}" Margin="10,2,2,2" HorizontalAlignment="Stretch" Tag="{Binding Path=ID}">
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}">
                                            <Setter Property="Background" Value="Gray" />
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="Orange" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我有多个问题。

1:我无法让扩展器中的按钮水平拉伸

2:如何将按钮的Tag属性设置为B的整个对象

3:为什么默认的鼠标悬停效果仍然执行。

谢谢

【问题讨论】:

  • 请将问题的标题更新为更有用的内容。如果这很难,您可能在一个主题中提出了太多问题。

标签: c# wpf button styles


【解决方案1】:

好的,到此为止:

1.你需要在ListBoxStackPanel里面设置HorizontalContentAlignment="Stretch"DataTemplate设置Orientation="Vertical"

2.Button上设置Tag="{Binding .}"

3. 将您的 Button.Style 更新为:

<Button.Style>
  <Style TargetType="{x:Type Button}">
    <Setter Property="Background"
            Value="Gray" />
    <Setter Property="OverridesDefaultStyle"
            Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
          <Border Background="{TemplateBinding Background}">
            <ContentPresenter />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <Trigger Property="IsMouseOver"
                Value="True">
        <Setter Property="Background"
                Value="Orange" />
      </Trigger>
    </Style.Triggers>
  </Style>
</Button.Style>

最后在添加问题之前4.记住ListBoxItem 有一个自己的Style。所以要完全覆盖MouseOver 上的效果,而不是Button 但在“项目”中,您必须为ListBox.ItemContainerStyle 提供自定义Style

哦,开始使用 Snoop。它可以帮助您调试布局问题。认为您可以用它解决问题 1,因为它会向您显示您的 ContentPresenterListBoxItem 正在使用所需的宽度而不是拉伸。

【讨论】:

  • 我同时找到了答案,但无法删除按钮边框。看起来您的解决方案也解决了这个问题 仍在学习 wpf。谢啦。你帮了很多忙
  • 另一件事,...如何在按钮上添加填充,以便文本和按钮之间有更大的空间。
  • 找到了。将值添加到 contentpresenter 的边距属性
  • @user853710 如果您需要其他帮助而不是添加到同一线程,请创建一个新问题。尽量将您的问题保留在 1 个问题上。
【解决方案2】:

只是快速浏览一下.. 对于问题 2,请尝试这样做:

<Button Content="{Binding Path=Name}" Margin="10,2,2,2" HorizontalAlignment="Stretch" Tag="{Binding}">

这应该使它绑定到 ListBox 中的项目。

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2012-02-23
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多