棒棒牛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2187|回复: 12

[解决] 奇怪的问题,使用代码设置按钮边框无效? [复制链接]

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-13 11:26:52 |显示全部楼层
如果在XMAL中设置按钮边框透明,按钮就没有边框:
   <Button.BorderBrush>
    <SolidColorBrush Color="#FFF30D0D"/>
   </Button.BorderBrush>
如果在代码中设置,就没有效果,不知为什么?
RepeatButton Bbtn1 = new RepeatButton();
Bbtn1.Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
Bbtn1.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255));

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-13 11:39:29 |显示全部楼层
这个问题是这样的:如果窗口无背景图,代码和XMAL设置的无边框(透明边框属性)都行,但是,如果有底图,就都不行了。
问题是:
在有背景图的情况下,如何设置按钮无背景并且无边框?

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
586
经验
1690
积分
2179
帖子
429

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-13 11:47:44 |显示全部楼层
给个Demo看看?
What is real?

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-13 13:13:32 |显示全部楼层
多谢Ray!!!

按照msdn论坛的办法,下面的方法没有按钮边框了。具体原理还在钻研中

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication3.MainWindow"
        x:Name="Window"
        Title="MainWindow"
        Width="640" Height="480">
    <Window.Background>
        <ImageBrush ImageSource="img8.jpg "/>
    </Window.Background>
        <Grid x:Name="LayoutRoot">
        <Button Width="30" Height="30">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <StackPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" x:Name="panel">
                            <ContentPresenter Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" />
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" Value="Gray" TargetName="panel" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="LightGray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
         </Style>
     </Button.Style>
     <Image Source="Images\Opetv.ico" />
</Button>
</Grid>
</Window>

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
586
经验
1690
积分
2179
帖子
429

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-13 13:58:20 |显示全部楼层
哦,这个是改了Button的模板,那肯定有效了
因为原来Button的模板里面是一个ButtonChrome,颜色和动画都是用代码写的
What is real?

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-13 15:38:19 |显示全部楼层
多谢 Ray!
起决定作用的是下列代码:  
<ControlTemplate TargetType="Button">
                        <StackPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" x:Name="panel">
                            <ContentPresenter Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" />
                        </StackPanel>

请教 Ray
:如何使用C#代码写这一小段?

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
586
经验
1690
积分
2179
帖子
429

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-13 17:17:51 |显示全部楼层
var controlTemplate = new ControlTemplate(typeof(Button));
            var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
            stackPanelFactory.SetValue(StackPanel.WidthProperty, new TemplateBindingExtension(Button.WidthProperty));
            stackPanelFactory.SetValue(StackPanel.HeightProperty, new TemplateBindingExtension(Button.HeightProperty));
            var contentPresenterFactory = new FrameworkElementFactory(typeof(ContentPresenter));
           ......(同上)
            stackPanelFactory.AppendChild(contentPresenterFactory);
            contentTemplate.VisualTree = stackPanelFactory;

不过为啥要用C#代码写呢?费力又不讨好。做成资源引用就行了啊
What is real?

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
586
经验
1690
积分
2179
帖子
429

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-13 17:18:54 |显示全部楼层
建议楼主再多看看WPF的基础知识
What is real?

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-13 18:22:26 |显示全部楼层
本帖最后由 gongxp 于 2010-9-14 09:38 编辑

回复 8# Ray


多谢RAY!
真是您的意思,做成资源应用,比如下面的例子:

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <Style x:Key="customButtonStyle" TargetType="Button">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
   
</Grid>

是可以的,如果在XMAL中创建一个按钮,并且按照Style="{StaticResource customButtonStyle}“应用这个 Style是可以的:
<Button x:Name="myButton" Width="50" Height="50" Style="{StaticResource customButtonStyle}">
        <Image Source="/SilverScholar;component/Resources/Sushi_099.png" Width="50" Height="50" />
    </Button>

但是,如果在代码中创建按钮,
Button Bbtn1 = new Button();
能后:
Bbtn1.Style = (Style)FindResource("customButtonStyle");
后一句就会异常,
如果:
  Style style = (Style)this.TryFindResource("customButtonStyle");
或者:
Style BtnStyle = new Style(typeof(Button));
BtnStyle = this.Resources["customButtonStyle"] as Style;
Bbtn1.Style = BtnStyle;
不报异常,但是没有效果。
因为要动态创建很多按钮,必须用代码实现, XMAL不适合。
再次感激不尽!!!
本人基础WPF薄弱,正在恶补中。

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
586
经验
1690
积分
2179
帖子
429

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-14 09:48:24 |显示全部楼层
因为你的Sytle是放在Grid的Resources中的,所以应该是this.LayoutRoot.FindResource("customButtonStyle")

另外,像这种大量创建重复按钮的代码,一般都可以转化成 数据绑定+DataTemplate,WPF里面直接创建控件的代码一般是比较少的。也许楼主可以看看自己的代码能否往这方面去改进 :)
What is real?
您需要登录后才可以回帖 登录 | 注册

bbniu.com (湘ICP备06008909号)  

GMT+8, 2012-5-22 21:34

Copyright © 2009-2011 bbniu.com. All Rights Reserved.

回顶部