如何获取控件模板中的图像源

发布于 2024-12-01 05:34:06 字数 1716 浏览 2 评论 0原文

我添加了一个像这样的按钮:

<Button x:Name="myButton" 
            Style="{StaticResource myButtonStyle}" 
            Height="36" 
            VerticalAlignment="Top"
            Click="myButton_Click">

        <Grid>
            <Image Height="*" Width="31" Source="{Binding Path=Image}" />
            <TextBlock Text="{Binding Path=DisplayName}" HorizontalAlignment="Center" />
        </Grid>
</Button>

其中“图像”是字符串中所需图像的来源。样式如下:

<Style x:Key="myButtonStyle"
     TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">

                        <Border x:Name="myButtonRootBorder">

                                <StackPanel Orientation="Horizontal">
                                    <Image Source="{??}" Width="{??}" Height="{??}" />
                                    <!--ContentPresenter-->
                                    <ContentPresenter Grid.Column="1"
                                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                    Content="{TemplateBinding Content}" />
                                </StackPanel
                        </Border>
                    </Grid>
                </ControlTemplate> 
            </Setter.Value>
        </Setter>
    </Style>

现在如何在控件模板中设置图像的高度、宽度和来源。

请帮忙。提前致谢。

I've added one button like this:

<Button x:Name="myButton" 
            Style="{StaticResource myButtonStyle}" 
            Height="36" 
            VerticalAlignment="Top"
            Click="myButton_Click">

        <Grid>
            <Image Height="*" Width="31" Source="{Binding Path=Image}" />
            <TextBlock Text="{Binding Path=DisplayName}" HorizontalAlignment="Center" />
        </Grid>
</Button>

Where 'Image' is the source of the required image in string. And the style is as follows:

<Style x:Key="myButtonStyle"
     TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">

                        <Border x:Name="myButtonRootBorder">

                                <StackPanel Orientation="Horizontal">
                                    <Image Source="{??}" Width="{??}" Height="{??}" />
                                    <!--ContentPresenter-->
                                    <ContentPresenter Grid.Column="1"
                                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                    Content="{TemplateBinding Content}" />
                                </StackPanel
                        </Border>
                    </Grid>
                </ControlTemplate> 
            </Setter.Value>
        </Setter>
    </Style>

Now how to set the height, width and source of the image in the control template.

Please help. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-12-08 05:34:06

对于宽度和高度,您可以使用 {TemplateBinding Width}{TemplateBinding Height}。如果您希望图像可自定义,您应该继承按钮类并添加 imagesource 属性:

public class ImageButton : Button {
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
}

然后您可以对图像使用 {TemplateBinding ImageSource}

For width and height you can use {TemplateBinding Width} and {TemplateBinding Height}. If you want the image to be customizable you should inherit the button class and add an imagesource property:

public class ImageButton : Button {
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
}

Then you can use {TemplateBinding ImageSource} for the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文