在代码中改变VisualBrush? [英] Change VisualBrush in code behind?

查看:104
本文介绍了在代码中改变VisualBrush?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我搜索更改图像按钮背景。

在Xaml代码中我使用:

Hello everybody,

I search to change image button background .
In Xaml code I use this :

<Button Style="{StaticResource CtrlButton}" Name="btnStart"  Height="64" Width="64" Margin="50,20,10,0" Click="btnStart_Click">
                    <Button.Background>
                        <VisualBrush Visual="{StaticResource Play}" Stretch="Uniform">
                          
                        </VisualBrush>
                    </Button.Background>
                </Button>





如何在代码后面更改图像?



How to change image in code behind?

推荐答案

我不知道你想做什么。如果你想改变像鼠标这样的事件的背景图像你可以使用datatrigger。



这个例子应该工作

I don''t know what you would like to do exactly . If you like to change the background image on an event like mouse over you can work with datatrigger.

This example should work
<Window x:Class="WpfApplication33.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BitmapImage x:Key="Source1" UriSource="../Resources/P1030224.JPG" />
        <BitmapImage x:Key="Source2" UriSource="../Resources/P1030220.JPG" />
        <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
            <Border>
                <Image Width="90" Height="90">
                    <Image.Style>
                        <Style TargetType="{x:Type Image}">
                            <Setter Property="Source" Value="{StaticResource Source2}" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Source" Value="{StaticResource Source1}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
            </Border>
        </ControlTemplate>
    </Window.Resources>

    <StackPanel>
        <Button Name="button1" Padding="10,2" Template="{StaticResource ButtonControlTemplate1}" />
    </StackPanel>
</Window>



重要提示:构建操作应该是资源。


Important: Build action should be resource.


制作两个重叠的按钮:



Make two overlayed buttons:

<window x:class="WpfApplication33.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <grid>
        <button name="button1" click="button1_Click" content="b1" padding="10,2" />
        <button name="button2" visibility="Collapsed" click="button2_Click" content="b2" padding="10,2" />
    </grid>
</window>





Codebehind:



Codebehind:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            button1.Visibility = Visibility.Collapsed;
            button2.Visibility = Visibility.Visible;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            button1.Visibility = Visibility.Visible;
            button2.Visibility = Visibility.Collapsed;
        }


您好,

感谢您的帮助。



我只想创建一个按钮Start / Stop,背景中有2个不同的图像,一个用于开始,一个用于停止。



实际上,我有一个不同图像的资源词典。这些图像是SVG文件转换。

我有一个样式的资源词典。对于我的按钮:

Hi,
Thanks for your help.

I just want create a button Start/Stop with 2 differents images in background, one to Start and one to Stop.

Actually, I have a ressource dictionnary with differents images. These images are SVG file conversion.
I have a ressource dictionnary for styles. For my buttons:
<pre lang="xml">
!-- Controle Button -->
    <style x:key="CtrlButton" targettype="{x:Type Button}" xmlns:x="#unknown">

     
     
        <setter property="Template">
            <setter.value>

                <controltemplate targettype="{x:Type Button}">
                    <border x:name="ButtonBorder">
                              CornerRadius="10,10,10,10" 
                              BorderThickness="0,0,0,0" 
                              BorderBrush="#FF8F8F8F"
                              RenderTransformOrigin="0.5,0.5"
                              Background="{TemplateBinding Background}"
                         
                    >
                      
                        
                    <contentpresenter x:name="ButtonContentPresenter">
                             VerticalAlignment="Bottom"  
                             HorizontalAlignment="Center"
                            
                    />
                        
                    </contentpresenter></border>
                    <controltemplate.triggers>
                        <trigger property="IsPressed" value="True">
                            <setter property="RenderTransform" targetname="ButtonBorder">
                                <setter.value>
                                    <transformgroup>
                                        <scaletransform scalex="0.9" scaley="0.9" />
                                    </transformgroup>
                                </setter.value>
                            </setter>
                        </trigger>
                    </controltemplate.triggers>
                </controltemplate>
                
            </setter.value>
        </setter>
    </style>



在我的Xaml代码中:


And in my Xaml code :

<button style="{StaticResource CtrlButton}" name="btnStart" height="64" width="64" margin="50,20,10,0" click="btnStart_Click">
                    <button.background>
                        <visualbrush visual="{StaticResource Play}" stretch="Uniform">
                          
                        </visualbrush>
                    </button.background>
                </button>





使用您的代码示例,图像只会改变鼠标在按钮上?



对于我的问题,我认为有必要仅通过代码更改背景图像。我会查看xaml示例代码。



With your code example, the image change only the mouse is over the button?

For my problem, i thought it was necessary to change the background image only by code. I would look in the xaml example code.


这篇关于在代码中改变VisualBrush?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆