gpt4 book ai didi

c# - 有人可以给我一些关于 WPF 的建议吗?

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:14 26 4
gpt4 key购买 nike

我正在制作一个数据绑定(bind)的用户控件。查询结果包括对象集合 (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: 我无法让扩展器中的按钮水平拉伸(stretch)

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

3:为什么默认的mouseover效果还是会执行。

谢谢

最佳答案

好的:

1. 你需要在ListBoxStackPanel里面设置Horizo​​ntalContentAlignment="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 上,而是在“Item”中,您必须为 提供自定义 Style >ListBox.ItemContainerStyle

哦,开始使用 Snoop .它可以帮助您调试布局问题。认为您可以用它解决问题 1,因为它会向您显示 ListBoxItemContentPresenter 使用的是必需的宽度,而不是拉伸(stretch)。

关于c# - 有人可以给我一些关于 WPF 的建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16234043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com