欢迎投稿

今日深度:

用SQL Server和XML学习Silverlight 2(1)(4)

控制器数据绑定 vs 事件处理程序

即使早期的XBAP项目几乎要完全依赖数据绑定,但在学习完这个领域的知识后,我们发现在Silverlight 2中的数据绑定仍然不够实用,既没有ElementName属性也没有Path属性,相反,我们使用了一系列的事件处理程序来设置TextBlock的文本属性,它与TextBox的为文本属性相当。

作者注记:要查看数据绑定在Silverlight中的地位,请参考silverlight.net上的这篇文章http://www.devx.com/RIA/link/38130)。

设置TextBlock的文本属性

private void txtFirstName_TextChanged
(object sender, TextChangedEventArgs e)
{//When the user types into the First Name textbox, 
add the same text in the First Name textblock
    txbFirstName.Text = txtFirstName.Text;
    PositionTextBlockInCanvas(txbFirstName); 
    //Adjust the location if it would fall 
     //outside the Imprint Area
    If (txbFirstName.Text.Length == 0)
    {//Also, unhide the And Character whenever the 
     //First Name textblock contains one or more letters
        txbAndCharacter.Visibility = Visibility.Collapsed;
    }
    else
    {
        txbAndCharacter.Visibility = Visibility.Visible;
    }
}

在这个示例代码中,AndCharacter TextBlock 的Visibility属性连接到FirstName TextBlock的文本属性Length了。当FirstName TextBlock中没有内容时,AndCharacter TextBlock就不可见,但当用户在FirstName TextBlock中输入字符时,AndCharacter TextBlock就自动被设为可见了。

改变水印颜色浮动TextBlock的Foreground属性)是通过事件处理程序完成的,MouseEnter事件程序创建一个SolidColorBrush,它匹配当前鼠标下方框内的颜色然后调用哪个ApplyNewImprintColor方法。

Silverlight 2改变浮动TextBlock的Foreground属性

<!-- Colored rectangles representing Drink Mate standard imprint colors  -->
   
            <Rectangle x:Name="ImprintColorRectangle1" 
Height="30" Width="30" Fill="#000000" Stroke="#FFFFFF" StrokeThickness="0" 
                 MouseEnter="handleMouseEnterImprintColors" 
MouseLeftButtonDown="handleImprintColorClick"
MouseLeave="handleMouseLeaveImprintColors" /><! --Black -->
            <Rectanglex:Name="ImprintColorRectangle2"
Height="30" 
Width="30" Fill="#000099" 
Stroke="#FFFFFF" 
StrokeThickness="0" 
                MouseEnter="handleMouseEnterImprintColors"  
MouseLeave
="handleMouseLeaveImprintColors" 
MouseLeftButtonDown
="handleImprintColorClick"  /><! --Reflex Blue -->
            <Rectangle x:Name="ImprintColorRectangle3" 
Height="30" Width="30" 
Fill="#0000CC" 
Stroke="#FFFFFF" 
StrokeThickness="0" 
                MouseEnter="handleMouseEnterImprintColors"  
MouseLeave="handleMouseLeaveImprintColors" 
MouseLeftButtonDown="handleImprintColorClick"  /><! -- Medium Blue -->
            <Rectangle x:Name="ImprintColorRectangle4" 
Height="30" Width="30" 
Fill="#3232FF" Stroke="#FFFFFF" StrokeThickness="0" 
                MouseEnter="handleMouseEnterImprintColors"  
MouseLeave="handleMouseLeaveImprintColors" 
MouseLeftButtonDown="handleImprintColorClick"  /><! -- Process Blue -->
private void handleMouseEnterImprintColors(object 
sender, MouseEventArgs e)
{
    Rectangle rect = (Rectangle)sender;            
    SolidColorBrush newImprintColor = (SolidColorBrush)rect.Fill;
    ApplyNewImprintColor(newImprintColor);
    bdrTooltips.Visibility = Visibility.Visible;
    txbTooltips.Text = "Click to change the imprint color.";
}
private void ApplyNewImprintColor(SolidColorBrush newImprintColor)
{
    try
    {
        txbFirstName.Foreground = newImprintColor;
        txbAndCharacter.Foreground = newImprintColor;
        txbSecondName.Foreground = newImprintColor;
        txbEventDate.Foreground = newImprintColor;
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}


www.htsjk.Com true http://www.htsjk.com/shujukujc/19157.html NewsArticle 控制器数据绑定 vs 事件处理程序 即使早期的XBAP项目几乎要完全依赖数据绑定,但在学习完这个领域的知识后,我们发现在Silverlight 2中的数据绑定仍然不...
评论暂时关闭