Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF/C MaterialDesignInXAML,如何将内容绑定到controlTemplate中的字符串_C#_Wpf_Material Design In Xaml - Fatal编程技术网

C# WPF/C MaterialDesignInXAML,如何将内容绑定到controlTemplate中的字符串

C# WPF/C MaterialDesignInXAML,如何将内容绑定到controlTemplate中的字符串,c#,wpf,material-design-in-xaml,C#,Wpf,Material Design In Xaml,当我想用IconPack使用按钮时 <Button Content="WindowMinimize" Style="{StaticResource WindowsControlButton}" /> 像这样。并通过controltemplate更改样式 <Style TargetType="{x:Type Button}" x:Key="WindowsControlButton" BasedOn="{StaticResource BaseStyle}"> <

当我想用IconPack使用按钮时

<Button Content="WindowMinimize" Style="{StaticResource WindowsControlButton}" />
像这样。并通过controltemplate更改样式

<Style TargetType="{x:Type Button}" x:Key="WindowsControlButton" BasedOn="{StaticResource BaseStyle}">
    <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Padding" Value="8"/>
    <Setter Property="Width" Value="45"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="Foreground" Value="{StaticResource ForegroundMainBrush}"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
                    <materialDesign:PackIcon Kind="{TemplateBinding Content}"/>                            // here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource BackgroundLightBrush}"/>
        </Trigger>
    </Style.Triggers>
</Style>
我试过这个。但不符合我的期望。 我怎么做?请帮帮我

我想使用按钮的内容更改ControlTemplate中的种类。
_

这里的问题是PackIcon.Kind和Button.Content之间在类型上的细微差别。前者是枚举,而后者是泛型对象。 在XAML中,当您键入时,它能够确定字符串WindowMinimize需要转换为目标类型PackIconKind,因为它应用于的属性。将字符串转换为适当的目标类型的过程由处理

在WPF中,按钮派生自ContentControl,因此Content属性只是一个通用对象。因此,当您设置时,它无法确定转换,因为内容可以是任何内容。它不知道如何将泛型对象转换为枚举。因此,即使Content属性中有字符串,它也不会尝试对目标枚举类型进行简单转换以外的任何操作,并且泛型字符串不会转换为枚举

有两种选择

正如上面的注释所建议的,您可以显式地将枚举值设置到内容中,类型应为MaterialDesignThemes.Wpf.PackIconKind。这是因为它将尝试将Content属性中的值强制转换为目标类型PackIconKind。 您可以编写一个值转换器来转换该值。这将要求您将绑定从TemplateBinding更改为相对源绑定,如下所示:
恐怕按你的方式不行。例如,您可以通过扩展类型为MahApps.Metro.IconPacks.PackIcon*Kind的Kind属性的按钮,并在模板中绑定到它来实现。它显示了什么错误?谢谢MaciekŚ。我用你的解决方案修复了。