Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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/2/visual-studio-2010/4.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
WPF:将XAML捕获到子类控件中_Wpf_Xaml - Fatal编程技术网

WPF:将XAML捕获到子类控件中

WPF:将XAML捕获到子类控件中,wpf,xaml,Wpf,Xaml,我使用XAML缩小了wpf按钮的外观 现在,我想创建一个子类按钮控件,我可以重用它,而不必编写所有标记 <Button Click="TestGridColumnButton_Click" Background="Transparent" Width="16" Height="16" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}"> <Button.Template> <Co

我使用XAML缩小了wpf按钮的外观

现在,我想创建一个子类按钮控件,我可以重用它,而不必编写所有标记

<Button Click="TestGridColumnButton_Click" Background="Transparent" Width="16" Height="16" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}">
    <Button.Template>
        <ControlTemplate>
            <Image HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource SourceStyle}" />
        </ControlTemplate>
    </Button.Template>
</Button>


如何使用C#设置所有这些属性

有多种方法可以做到这一点:

[注意:为了回答这个问题,我假设您正在一个简单的窗口中创建一个按钮。]

1) 一种不可重用且肮脏的方法是分配窗口已加载事件中的所有属性。通过命名button对象,您可以获取对它的引用,并为属性指定值,注册事件并指定模板。我想您在模板属性方面遇到了问题。要在代码中创建模板,可以使用(不推荐使用)或使用XamlReader.Load方法(这是首选方法)

2) 创建自定义控件,在generic.xaml文件中为其创建自定义模板。您可以命名自定义模板中的元素,并通过重写OnApplyTemplate方法检索对它们的引用。然后,可以创建直接映射到模板中图像元素的公共属性,或者将图像元素作为控件中的公共属性公开。使用这种方法,您可以使用公开的属性在C代码中设置属性

3) 您还可以在c代码中定义多种样式并在它们之间切换。当我们使用JavaScript切换类时,此方法的工作方式与HTML中的工作方式类似


希望这有帮助

有多种方法可以做到这一点:

[注意:为了回答这个问题,我假设您正在一个简单的窗口中创建一个按钮。]

1) 一种不可重用且肮脏的方法是分配窗口已加载事件中的所有属性。通过命名button对象,您可以获取对它的引用,并为属性指定值,注册事件并指定模板。我想您在模板属性方面遇到了问题。要在代码中创建模板,可以使用(不推荐使用)或使用XamlReader.Load方法(这是首选方法)

2) 创建自定义控件,在generic.xaml文件中为其创建自定义模板。您可以命名自定义模板中的元素,并通过重写OnApplyTemplate方法检索对它们的引用。然后,可以创建直接映射到模板中图像元素的公共属性,或者将图像元素作为控件中的公共属性公开。使用这种方法,您可以使用公开的属性在C代码中设置属性

3) 您还可以在c代码中定义多种样式并在它们之间切换。当我们使用JavaScript切换类时,此方法的工作方式与HTML中的工作方式类似


希望这有帮助

为按钮创建样式并添加到全局资源字典中

<Application x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="MyIconButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Transparent" />
                <Setter Property="Width" Value="16" />
                <Setter Property="Height" Value="16" />
                <Setter Property="Margin" Value="0,0,0,0" />
                <Setter Property="Padding" Value="0,0,0,0" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="buttonImage.jpg"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

使用如下样式:

  <Button Style="{StaticResource MyIconButtonStyle}" Click="Button_Click"/>

为按钮创建样式并添加到全局资源字典中

<Application x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="MyIconButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Transparent" />
                <Setter Property="Width" Value="16" />
                <Setter Property="Height" Value="16" />
                <Setter Property="Margin" Value="0,0,0,0" />
                <Setter Property="Padding" Value="0,0,0,0" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="buttonImage.jpg"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

使用如下样式:

  <Button Style="{StaticResource MyIconButtonStyle}" Click="Button_Click"/>


hmmm。。。我想我可以用这个按钮创建一个WPF用户控件,然后重新使用它。。那会更好吗?嗯。。。我想我可以用这个按钮创建一个WPF用户控件,然后重新使用它。。这会更好吗?这里是对Xaml.Load方法的引用:酷,谢谢。。是的,我记得一个样式可以用模板创建。。我从来没有这样做:)这里是对Xaml.Load方法的引用:很酷,谢谢。。是的,我记得一个样式可以用模板创建。。我只是从来没有这样做过:)