积极答复者
Button设置BorderBrush失败

问题
-
大家好,
1、我试图设置BorderBrush,但是失败了。
2、根据Textblock是否有内容,按钮enable状态显示是正确的。
但是通过调试发现,所有"转换器"都是成功的。
一、XAML:
1、button的enable绑定到另一个textblock的text,如果text为空则按钮处于禁用状态,反之按钮状态为enable。
2、button的BorderBrush绑定到button的IsEnable属性,如果button为enable,则BorderBrush为红色,反之BorderBrush为灰色。
<Button x:Name="button_addText" Content="测试" IsEnabled="{Binding Text, ElementName=textbox_addNewText, Converter={StaticResource AddNewText_converter}}" BorderBrush="{Binding IsEnabled, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource ButtonBorderColor_converter}}" BorderThickness="2" />
二、转换器(Debug发现调用是是成功的)
public class AddNewTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { string newText = value as string; if (string.IsNullOrEmpty(newText )) { return false; } return true; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } public class ButtonBorderColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { bool IsEnable = (bool)value; if (IsEnable) { return Colors.Red; } return Colors.LightGray; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } }
- 已编辑 oneonce 2015年4月14日 2:37
答案
-
给你分享个我自用的代码吧
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace myapp{
class RGBTrans
{
//参数格式"#ff000000"
public static SolidColorBrush getARGBColor(string colorStr)
{
byte byteA = System.Byte.Parse(colorStr.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
byte byteR = System.Byte.Parse(colorStr.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
byte byteG = System.Byte.Parse(colorStr.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
byte byteB = System.Byte.Parse(colorStr.Substring(7, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
SolidColorBrush color = new SolidColorBrush(Color.FromArgb(byteA, byteR, byteG, byteB));
return color;
}
//参数格式"/Img/test.png"
public static ImageBrush getImageBrush(string path)
{
ImageBrush img = new ImageBrush();
img.ImageSource = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
return img;
}
}
}两个方法,一个返回color类型的东西,一个返回imagebrush
- 已标记为答案 oneonce 2015年4月14日 9:15
全部回复
-
给你分享个我自用的代码吧
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace myapp{
class RGBTrans
{
//参数格式"#ff000000"
public static SolidColorBrush getARGBColor(string colorStr)
{
byte byteA = System.Byte.Parse(colorStr.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
byte byteR = System.Byte.Parse(colorStr.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
byte byteG = System.Byte.Parse(colorStr.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
byte byteB = System.Byte.Parse(colorStr.Substring(7, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
SolidColorBrush color = new SolidColorBrush(Color.FromArgb(byteA, byteR, byteG, byteB));
return color;
}
//参数格式"/Img/test.png"
public static ImageBrush getImageBrush(string path)
{
ImageBrush img = new ImageBrush();
img.ImageSource = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
return img;
}
}
}两个方法,一个返回color类型的东西,一个返回imagebrush
- 已标记为答案 oneonce 2015年4月14日 9:15