2013-04-26 59 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中的項目。

相關問題