2015-06-26 33 views
1

我有一個UserControl多用途使用。 爲了簡單起見,我會告訴你的控制第一:WPF UserControl:自定義樣式的TargetType的客戶端驗證?

<UserControl x:Class="CompetitionAgent.View.UserControls.ExpandingButtonGrid" 
      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:DesignWidth="200" d:DesignHeight="200" 
      Margin="0" Padding="0" Width="Auto" Height="Auto" > 
    <StackPanel Name="stpBody" Style="{Binding Style}"> 
     <Button x:Name="btnExpander" Content="{Binding ExpanderButtonText}" 
       Style="{Binding ExpandButtonStyle}" 
       HorizontalAlignment="Center" Click="btnExpander_Click" 
       Height="25" Width="Auto" /> 
     <StackPanel x:Name="stpButtons" Orientation="Horizontal" 
        Style="{Binding PanelStyle}" 
        Margin="0"> 
     </StackPanel> 
    </StackPanel> 
</UserControl> 

控制stpBodystpButtonsbtnExpander都被DataContext約束樣式。該領域是這樣的:

#region body styles 
public Style Style { get; set; } 
public Style ExpandButtonStyle { get; set; } 
#endregion body styles 

#region button pannel styles 
public Style PanelStyle { get; set; } 
public Style ButtonStyle { get; set; } 
#endregion button pannel styles 

所以,當這UserControl在另一個窗口時,它看起來有點像這樣:

<UserControls:ExpandingButtonGrid x:Name="ebgSchemeManager" 
            Style="{StaticResource ExpandingButtonGridStyle}" 
            PanelStyle="{StaticResource ExpandingButtonGridPanelStyle}" 
            ExpandButtonStyle="{StaticResource ExpandingButtonGridExpandButtonStyle}" /> 

我想知道,有沒有辦法來驗證的TargetTypesStaticResource樣式,以便它們需要分別指向一個堆疊面板或按鈕?

例如,樣式ExpandingButtonGridExpandButtonStyle可能以DockPanel爲目標,導致運行時出現XAML分析異常。

UPDATE - 總結和更新上Depency對象

我剛剛想出什麼DepencyObjects是(萬歲!)。 對於那些想知道的人,您需要註冊該字段,以便可以動態分配該屬性。

public static readonly DependencyProperty ExpandButtonStyleProperty = 
    DependencyProperty.Register("ExpandButtonStyle", typeof(Style), typeof(ExpandingButtonPanel)); 

public Style ExpandButtonStyle 
{ 
    get 
    { 
     return (Style)GetValue(ExpandButtonStyleProperty); 
    } 
    set 
    { 
     if (!typeof(Button).IsAssignableFrom(value.TargetType)) 
     { 
      throw new ArgumentException("The target type is expected to be button"); 
     } 
     SetValue(ExpandButtonStyleProperty, value); 
    } 
} 
+1

您可以驗證它的風格設置,並拋出異常(當您試圖這樣做會在瞬間設計師可見)。對於這個'Style' /'PanelStyle' /等。必須是*依賴屬性*。 – Sinatr

+0

@Sinatr巧合的是,我剛剛收到這個錯誤'A'DynamicResourceExtension'只能在DependencyObject的DependencyProperty上設置。'我必須看看,謝謝! – DerpyNerd

回答

1

當然有。你可以驗證你的設置器中的Style.TargetType。這也將被WPF設計器檢查。

例如:

private Style _buttonStyle; 
public Style ButtonStyle 
{ 
    get 
    { 
     return _buttonStyle; 
    } 
    set 
    { 
     if (!typeof(Button).IsAssignableFrom(value.TargetType)) 
     { 
      throw new ArgumentException("The target type is expected to be Button"); 
     } 
     _buttonStyle = value; 
    } 
} 
+0

嗨,謝謝,我會馬上嘗試你的解決方案來驗證。謝謝! – DerpyNerd

+1

如果在C#語法中有任何錯誤...請告訴我。我通常使用VB.net,但如果需要我想修復答案。 – Nitram

+1

它的工作原理,但你忘記了分號'return _buttonStyle;'和setter的右括號之後的分號不應該存在'_buttonStyle = value; }'。可以理解,如果你習慣了VB.net;) – DerpyNerd

相關問題