2013-11-21 27 views
1

我不明白如何定義WPF中的佈局,這取決於所提供的數據。這是我得到的:http://imgur.com/Ozgka7d(工作示例的屏幕截圖)。WPF佈局依賴於數據

我使用的樣品數據是:

<model:QuestionCollection xmlns:model="clr-namespace:Reuma_Toolbox.Model"> 
    .... 
    <model:Question Title="En groente?"> 
     <model:Question.Answers> 
      <model:AnswerCollection RadioButton="True"> 
       <model:Answer Id="spinach" IsChecked="True" /> 
       <model:Answer Id="patato" /> 
      </model:AnswerCollection> 
     </model:Question.Answers> 
    </model:Question> 
    <model:Question Title="Hoe vaak eet je spinazie, toe zeg vertel het me!" > 
     <model:Question.Answers> 
      <model:AnswerCollection RadioButton="False"> 
       <model:Answer Id="0" Label="1" Title="Zonder enige moeilijkheid" /> 
       <model:Answer Id="1" Label="2" Title="Elke dag" /> 
       <model:Answer Id="2" Label="3" Title="Nooit" IsChecked="True"/> 
       <model:Answer Id="3" Label="4" Title="Onmogelijk" /> 
      </model:AnswerCollection> 
     </model:Question.Answers> 
    </model:Question> 
    .... 
</model:QuestionCollection> 

所以基本上的問題列表,每個問題具有任一組複選框或一組單選按鈕。

我需要的是,如果RadioButton =「False」,佈局從單選按鈕的水平行更改爲複選框和標籤的垂直列表。我真的不知道如何做到這一點。觸發器?不同的風格?

的WPF我現在有是這樣的:

<report:BlockUserControl x:Class="Reuma_Toolbox.Reports.QuestionsControl" 
      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" 
      xmlns:report="clr-namespace:Reuma_Toolbox.Reports" 
      mc:Ignorable="d" 
      d:DesignHeight="210" d:DesignWidth="430"> 

    <report:BlockUserControl.Resources> 
     <Style x:Key="answerRectangle" TargetType="Rectangle"> 
      <Setter Property="Width" Value="16"/> 
      <Setter Property="Height" Value="16"/> 
      <Setter Property="Fill" Value="{DynamicResource DarkGrayBrush}"/> 
      <Setter Property="Margin" Value="3"/> 
     </Style> 
    </report:BlockUserControl.Resources> 

    <ItemsControl d:DataContext="{d:DesignData /SampleData/QuestionsSampleData.xaml}" ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid MinHeight="50" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}}"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition/> 
         <ColumnDefinition Width="Auto"/> 
        </Grid.ColumnDefinitions> 
        <Separator Grid.Row="0" Grid.ColumnSpan="2" Margin="0,2" Background="{StaticResource LightGrayBrush}" /> 
        <TextBlock Grid.Row="1" Grid.Column="0" TextWrapping="Wrap" FontSize="12"> 
         <TextBlock.Text> 
          <MultiBinding Converter="{StaticResource stringConcatConverter}"> 
           <Binding Path="Title" /> 
           <Binding Path="Subtitle" /> 
          </MultiBinding> 
         </TextBlock.Text> 
        </TextBlock> 
        <ItemsControl Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Answers}" Margin="10,0,2,0" VerticalAlignment="Top"> 
         <ItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <UniformGrid Rows="1"/> 
          </ItemsPanelTemplate> 
         </ItemsControl.ItemsPanel> 
         <ItemsControl.ItemTemplate> 
          <DataTemplate> 
            <Rectangle x:Name="square" Style="{StaticResource answerRectangle}" /> 
           <DataTemplate.Triggers> 
            <DataTrigger Binding="{Binding Title}" Value="{x:Null}"> 
             <Setter TargetName="square" Property="Fill" Value="{StaticResource LightGrayBrush}"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding IsChecked}" Value="True"> 
             <Setter TargetName="square" Property="Fill" Value="{StaticResource DarkBlueBrush}"/> 
            </DataTrigger> 
           </DataTemplate.Triggers> 
          </DataTemplate> 
         </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</report:BlockUserControl> 
+0

你可以嘗試雙向數據綁定到這些屬性 –

回答