2013-04-26 34 views
2

我正在做一个数据绑定的用户控件。查询结果包括一个对象的集合(A),其中A与其他结果的碰撞(B)。所以A包含多个B.有人可以给我一些WPF的建议吗?

在用户控件中我想表示集合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:如何设置按钮的标签属性为B的整个对象

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

感谢

+0

请将问题的标题更新为更有用的内容。如果这很难,你可能会在一个主题中提出太多问题。 – 2013-04-26 11:33:24

回答

0

确定这里有云:

您需要设置的ListBoxHorizontalContentAlignment="Stretch"并在StackPanelDataTemplate设置Orientation="Vertical"

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

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正在使用所需的宽度而不是拉伸。

+0

我在此期间找到了答案,但无法删除按钮boder。看起来你的解决方案也可以解决这个问题 仍在学习wpf。 谢谢你。你一直有很多的坚持 – user853710 2013-04-26 12:11:42

+0

另一件事,...我如何添加一个填充按钮,使文本和按钮之间有更大的空间。 – user853710 2013-04-26 14:12:30

+0

找到了。将该值添加到contentpresenter的margin propery – user853710 2013-04-26 14:15:18

0

就采取快速看一下吧..问题2试试这样做:

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

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

相关问题