2012-10-21 48 views
0

我想下面寫一個條件控制:條件塊在XAML

<local:ConditionalBlock Condition={Binding CertainBoolValue}> 
    <ConditionalBlock.Match> 
     <!-- Any content here --> 
    </ConditionalBlock.Match> 
    <ConditionalBlock.Else> 
     <!-- Any content here --> 
    </ConditionalBlock.Else> 
</local:ConditionalBlock> 

此刻,我不知道應如何實施。所以請幫助。由於

編輯

在等待迴應,我已經使用自定義控制&控制模板來實現自己的解決方案如下:

using System.Windows; 
using System.Windows.Controls; 

namespace Sample 
{ 
    public class ConditionalControl : ContentControl 
    { 
     static ConditionalControl() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(ConditionalControl), new FrameworkPropertyMetadata(typeof(ConditionalControl))); 
     } 

     public object Alternative 
     { 
      get { return (object)GetValue(AlternativeProperty); } 
      set { SetValue(AlternativeProperty, value); } 
     } 

     public static readonly DependencyProperty AlternativeProperty = 
      DependencyProperty.Register("Alternative", typeof(object), typeof(ConditionalControl), new UIPropertyMetadata(null)); 

     public bool Condition { 
      get { return (bool)GetValue(ConditionProperty); } 
      set { SetValue(ConditionProperty, value); } 
     } 

     public static readonly DependencyProperty ConditionProperty = 
      DependencyProperty.Register("Condition", typeof(bool), typeof(ConditionalControl), new UIPropertyMetadata(null)); 
    } 
} 

ConditionalControl.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Sample"> 

    <Style TargetType="{x:Type local:ConditionalControl}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:ConditionalControl}"> 
        <Grid> 
         <ContentPresenter Name="match"/> 
         <ContentPresenter Name="alternative" ContentSource="Alternative" /> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="Condition" Value="True"> 
          <Setter TargetName="match" Property="Visibility" Value="Visible"/> 
          <Setter TargetName="alternative" Property="Visibility" Value="Collapsed"/> 
         </Trigger> 
         <Trigger Property="Condition" Value="False"> 
          <Setter TargetName="match" Property="Visibility" Value="Collapsed"/> 
          <Setter TargetName="alternative" Property="Visibility" Value="Visible"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

樣品用法:

<Grid> 
    <local:ConditionalControl Condition="{Binding CertainBoolValue}"> 
     <Label>Match case</Label> 
     <local:ConditionalControl.Alternative> 
      <Label>Alternative case</Label> 
     </local:ConditionalControl.Alternative> 
    </local:ConditionalControl> 
</Grid> 

無論如何,感謝Luke Woodward的及時迴應。我還是選你的答案接受

+2

你看過DataTriggers嗎?您可以將DataTrigger放入基於條件 – cordialgerm

+0

使用不同ControlTemplates的樣式中。使用此處所述的轉換器可以獲得/類似/功能:http://blog.greatrexpectations.com/2012/10/04/getting-rid -of-xaml-boilerplate/ –

+0

@pickles:謝謝,我在我的解決方案中使用了ControlTemplate.Triggers – CEDA

回答

1

的一種方式做,這是創建具有以下XAML一個用戶控件:

<ContentControl x:Class="WpfApplication1.ConditionalBlock" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" /> 

和下面的代碼隱藏:

using System.Windows; 
using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    public partial class ConditionalBlock : ContentControl 
    { 
     public static readonly DependencyProperty ConditionProperty = 
      DependencyProperty.Register("Condition", typeof(bool), typeof(ConditionalBlock), 
             new FrameworkPropertyMetadata(Condition_Changed)); 

     public ConditionalBlock() 
     { 
      InitializeComponent(); 
      Loaded += ((s, e) => UpdateContent()); 
     } 

     public bool Condition 
     { 
      get { return (bool)GetValue(ConditionProperty); } 
      set { SetValue(ConditionProperty, value); } 
     } 

     public object Match { get; set; } 

     public object Else { get; set; } 

     private static void Condition_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
     { 
      var conditionalBlock = obj as ConditionalBlock; 
      if (conditionalBlock != null) 
      { 
       conditionalBlock.UpdateContent(); 
      } 
     } 

     private void UpdateContent() 
     { 
      Content = (Condition) ? Match : Else; 
     } 
    } 
} 

注意我們已將UserControl的超類更改爲ContentControl

這對條件使用依賴項屬性,以便在約束值更改時通知我們。當條件改變時,我們通過分配Content依賴項屬性來更新我們從ContentControl繼承的依賴屬性,從而更新哪些內容可見。最後,爲確保首次顯示控件時顯示一個或其他內容,我們更新Loaded事件的處理程序中的內容。 (從內部構造不起作用調用它,因爲在這一點上MatchElse性質尚未確定。)

我假定MatchElse屬性的內容不會改變,所以我沒有打算將它們作爲依賴屬性來實現。