2016-10-24 20 views
2

我需要將一個按鈕綁定到控件模板。在XAML看起來是這樣的:綁定到控件模板

Button Template="{Binding Status, Converter={StaticResource StatustoTemplate}}" 

轉換器(StatustoTemplate)運行良好的狀態(這是一個整數,但很高興爲它是一個字符串)的變化:

public class StatustoTemplate : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 

     { 
      if (value==1) 
      { 
       return ControlTemplateName1; 
      } 
      if (value==2) 
      { 
       return ControlTemplateName2; 
      } 
     } 
} 

現在,我可以以什麼格式發回ControlTemplate1ControlTemplate2?讓我們假設ControlTemplate1ControlTemplate2是在XAML中定義的有效控制模板。我現在需要返回一個ControlTemplate - 但是如何設置它?

回答

0

轉換器不容易找到XAML中定義的資源。我通常定義一個具有兩種定義的控制模板,並使用Visibility來切換它們。該代碼只是一個簡短的示例。

XAML

<Window> 
    <Window.DataContext> 
     <local:MainWindowViewModel/> 
    </Window.DataContext> 
    <Window.Resources> 
     <ResourceDictionary> 
      <local:MyConverter x:Key="MyConverter"/> 
      <ControlTemplate x:Key="template"> 
       <Grid> 
        <Grid Visibility="{Binding Status, Converter={StaticResource MyConverter}, ConverterParameter=1}"> 
         <TextBox Text="1"/> 
        </Grid> 
        <Grid Visibility="{Binding Status, Converter={StaticResource MyConverter}, ConverterParameter=2}"> 
         <TextBox Text="2"/> 
        </Grid> 
       </Grid> 
      </ControlTemplate> 
     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <Button Template="{StaticResource template}"/> 
    </Grid> 
</Window> 

轉換

public class MyConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ((string)parameter == "1") ? Visibility.Visible : Visibility.Collapsed; 
    } 

    // ConvertBack 
} 
0

我有一個MarkupExtension,可能爲你工作。這是得到xaml定義ResourcesResourceKey

第一:抽象類StyleRefExtension:

public abstract class StyleRefExtension : MarkupExtension 
{ 
    protected static ResourceDictionary RD; 
    public string ResourceKey { get; set; } 

    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     return ProvideValue(); 
    } 

    public object ProvideValue() 
    { 
     if (RD == null) 
      throw new Exception(
       @"You should define RD before usage. 
      Please make it in static constructor of extending class!"); 
     return RD[ResourceKey]; 
    } 
} 

二:類實現爲StyleRefExt

public class StyleRefExt : StyleRefExtension 
{ 
    static StyleRefExt() 
    { 
     RD = new ResourceDictionary 
      { 
       Source = new Uri("pack://application:,,,/##YOUR_ASSEMBLYNAME##;component/Styles/##YOUR_RESOURCE_DICTIONARY_FILE##.xaml") 
      }; 
    } 
} 

只需更換## YOUR_ASSEMBLYNAME ##與程序集的名稱, ## YOUR_RESOURCE_DICTIONARY_FILE ##與您的文件名r ResourceDictionary(位於文件夾Styles)。

Converter應該是這樣的:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    if (value==1) { 
     StyleRefExt sr = new StyleRefExt {ResourceKey = "ControlTemplateName1"}; 
     return sr.ProvideValue(); 
    } 
    if (value==2) { 
     StyleRefExt sr = new StyleRefExt {ResourceKey = "ControlTemplateName2"}; 
     return sr.ProvideValue(); 
    } 
} 
0

我的首選方法是使用樣式與DataTriggers切換模板,無需轉換器

<Style TargetType="Button" x:Key="StatusButton"> <!--set BasedOn if there is a base Style--> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Status}" Value="1"> 
      <Setter Property="Template" Value="{StaticResource ControlTemplateName1}"/> 
     </DataTrigger> 

     <DataTrigger Binding="{Binding Status}" Value="2"> 
      <Setter Property="Template" Value="{StaticResource ControlTemplateName2}"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

,然後應用此風格:

<Button Style="{StaticResource StatusButton}"/>