---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
1.要实现数据的绑定,需要在ColorString类中实现接口INotifyPropertyChanged,它包含一个名叫PropertyChanged的事件成员。
然后我们在属性的set访问器中触发事件。*注意:接口不是在FileOperator或Rename中实现,ColorString的成员才是要被绑定的成员(还记得我们使用ColorString类型来保存数据吗?)
/// <summary>现在,我们只需要在窗口初始化的时候设置好数据上下文就可以把数据绑定到控件上了
/// 这个类用于创建带有颜色的文本
/// </summary>
public class ColorString : INotifyPropertyChanged
{
private string text;
private TextColor color;
private bool visibility;
public event PropertyChangedEventHandler PropertyChanged;
public TextColor Color //颜色属性
{
get
{
return color;
}
set
{
color = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Color"));
}
}
}
public string Text // 文本属性
{
get { return text; }
set
{
text = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
}
public bool Visibility //可见性
{
get
{
return visibility;
}
set
{
visibility = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Visibility"));
}
}
}
}
private void OneClick_Loaded(object sender, RoutedEventArgs e)
{
// 设置数据上下文
TabItem_Rename.DataContext = Rename.Default;
}
<TextBox Text="{Binding Path=DirectoryPath.Text}" IsReadOnly="True" Name="TBox1_ShowPath" Width="300"></TextBox>
但是,问题来了,我们发现,虽然TextBox的Text属性和Rename.Default.Text属性都是string类型不需要转换。但Foreground属性是一个System.Windows.Media.Brush类型,而我们的的Color属性是一个FileOperator.ColorString.TextColor类型!它们无法进行类型转换!
同样,System.Windows.Visibility类型和我们的Visibility属性的bool类型之间也不能。
<TextBox Foreground="Black" Visibility="Visible"></TextBox>为了解决属性之间类型转换的问题,我们需要定义两个 转换类,它们的命名规范一般为 **To**Converter, 这些类实现了IValueConverter接口。这个接口包含的两个函数成员用于类型的转换。 我把它定义在
namespace OneClick_UI.common
{
class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool b = (bool)value;
return b ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility v = (Visibility)value;
return v == Visibility.Visible;
}
}
}
namespace OneClick_UI.common{ class TextColorToSolidColorBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //暂时想不出好方法,只能用硬编码 switch ((OneClick_BLL.FileOperator.ColorString.TextColor)value) { case OneClick_BLL.FileOperator.ColorString.TextColor.Red: return new SolidColorBrush(System.Windows.Media.Colors.Red); case OneClick_BLL.FileOperator.ColorString.TextColor.Black: return new SolidColorBrush(System.Windows.Media.Colors.Black); case OneClick_BLL.FileOperator.ColorString.TextColor.Orange: return new SolidColorBrush(System.Windows.Media.Colors.Orange); default: throw new ApplicationException("value找不到对应的case,是否缺少case标签"); } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var color = (SolidColorBrush)value; if (color.Color == System.Windows.Media.Colors.Red) { return OneClick_BLL.FileOperator.ColorString.TextColor.Red; } else if (color.Color == System.Windows.Media.Colors.Black) { return OneClick_BLL.FileOperator.ColorString.TextColor.Black; } else if (color.Color == System.Windows.Media.Colors.Orange) { return OneClick_BLL.FileOperator.ColorString.TextColor.Orange; } else { throw new ApplicationException("OneClick_BLL.FileOperator.ColorString.TextColor没有定义对应System.Windows.Media.Colors类型的颜色"); } } }}
然后,我们还需要在XAML中引用命名空间和添加资源
<Window x:Name="OneClick" x:Class="OneClick_UI.MainWindow"
xmlns:common="clr-namespace:OneClick_UI.common"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
Title="One Click" Height="535" Width="553" Loaded="OneClick_Loaded">
<Grid.Resources> <common:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"></common:BoolToVisibilityConverter> <common:TextColorToSolidColorBrushConverter x:Key="textColorToSolidColorBrushConverter"></common:TextColorToSolidColorBrushConverter> </Grid.Resources>最后,我们只需要把控件绑定到属性上的同时添加一个转换器(Converter)就可以了,下面是全部的XAML代码
<TextBox Foreground="{Binding Path=DirectoryPath.Color, Converter={StaticResource textColorToSolidColorBrushConverter}}" Text="{Binding Path=DirectoryPath.Text}" IsReadOnly="True" Name="TBox1_ShowPath" Width="300"></TextBox>
<TextBox Name="TBox_FileType" Foreground="{Binding Path=SpecifiedType.Color, Converter={StaticResource textColorToSolidColorBrushConverter}}" Text="{Binding Path=SpecifiedType.Text, Mode=TwoWay}" Width="50" IsEnabled="True"></TextBox>
<TextBox Foreground="{Binding Path=CustomName.Color, Converter={StaticResource textColorToSolidColorBrushConverter}}" Text="{Binding Path=CustomName.Text}" Width="100"></TextBox>
<TextBlock TextWrapping="Wrap" Name="TBlock_Message" Text="{Binding Path=Message.Text}" Foreground="{Binding Path=Message.Color, Converter={StaticResource textColorToSolidColorBrushConverter}}" Height="48" Margin="2,5,2,0" FontSize="16" Visibility="{Binding Path=Message.Visibility, Converter={StaticResource boolToVisibilityConverter}}"></TextBlock>
这就是这个小软件的全部 关于数据绑定的内容。
下一篇文章开始我将讲解这个软件最有价值的地方(自称),敬请期待。
源代码在我写完所有文章后会整理发布。初试牛刀,希望能得到大家的鼓励和指导,请大家多多指教。
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://www.itheima.com" target="blank">www.itheima.com</a>