繁体   English   中英

绑定到控件模板

[英]Binding to a Control Template

我需要将按钮绑定到控件模板。 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 - 但是如何设置它?

我的首选方法是使用带有 DataTriggers 的 Style 来切换模板,而无需转换器

<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}"/>

转换器不容易找到 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
}

我有一个可能适合你的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##替换为您的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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM