我也来说一下SilverLight3的ChildWindow带Logo的标题

      今天我谈的是SilverLight3的ChildWindow的标题问题,我想在ChildWindow标题前面加上一个图片,比如logo,如下图的效果,标题前面有一个图标。

     

  要实现以上的效果,有两种方法可以实现。

      1、由于ChildWindow的标题(Title)可以设置为任何对象。这样,您可以创建包含文本、图形和动画的复杂标题。如果该对象不具有直观表示形式,则 ToString 方法返回的对象的字符串表示形式将显示在标题栏中,其实Title有这种功能,还是主要归功于显示标题的元素ContentControl类。

         ContentControl类表示包含单项内容的控件。像Button,CheckBox和ScrollView 这样的控件直接或间接继承自该类,ContentControlContent 属性可以是任何类型的对象,例如字符串、UIElementDateTime。当 Content 设置为 UIElement 时,ContentControl 中将显示 UIElement。当 Content 设置为其他类型的对象时,ContentControl 中将显示该对象的字符串表示形式。

       通过上面的说明,大家已经知道ChildWindow的标题为什么可以包含文本、图形和动画了吧。那么知道这个原理之后,实现就很简单了。下面就是在CS中实现的代码。你可以把代码写到构造函数中即可

      

代码
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace ChildMetadataApp
{
    
public partial class ChildMetadata:ChildWindow
    {
        Image ig;
        
public ChildMetadata()
        {
            InitializeComponent();

            
//定义一个StackPanel,并且水平排列两个元素,一个是Image(即Logo),一个是TextBlock(即标题)

            StackPanel sp 
= new StackPanel();

            
//设置为水平排列显示
            sp.Orientation = Orientation.Horizontal;
            
if (ig != null)
            {
                sp.Children.Add(ig);
            }
            
else
            {
                
//如果设置图片,显示默认图片
                ig = new Image();
                ig.Source 
= new BitmapImage(new Uri("../Resources/Image/waiting.png", UriKind.RelativeOrAbsolute));
                sp.Children.Add(ig);
            }

            
//显示文字标题

            TextBlock tb1 
= new TextBlock();
            tb1.Text 
= Convert.ToString(this.Title);
            sp.Children.Add(tb1);

            
//把StackPanel 赋值给ChildWindow的Title即可
            this.Title = sp;
        }
    }
}

  通过以上代码就可以实现上图中的效果,不过此方法比较麻烦,如果经常使用ChildWindow的话,必须得在每个 ChildWindow下复制这段代码。那么可以使用下面的方法来实现--扩展ChildWindow.

  2、第二种方法就是扩展ChildWindow

          首先新建一个工程ExtendChildWindow,然后在工程新建一个类ExtendChildWindow,这个类继承ChildWindow.然后重写方法OnApplyTemplate。

          下面是文件ExtendChildWindow.cs中的代码

          

代码
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace ExtendChildWindows{
    
public class ExtendChildWindows : ChildWindow //继承ChildWindow
{
       /定义变量
        rid g;
     //ContentControl类,已经介绍过这个类
      ContentControl cc 
= new ContentControl();
        Image ig
=null;
        
public Image DefineImage
        {
            
get { return ig; }
            
set { ig = value; }
        }
        
public ExtendChildWindows(): base()
        {

        }
        
        
public override void OnApplyTemplate()
        {
            
base.OnApplyTemplate();
            //取得标题栏的Grid,其中Grid分为两列,一列是ContentControl,显示标题,另一列为Button,显示关闭按钮。只要取得ContentControl即可。
            g 
= (GetTemplateChild("Chrome"as Border).Child as Grid;
       //得到ContentControl
            cc 
= g.Children[0as ContentControl;
            //以下代码和第一种方法的代码一样
            StackPanel sp 
= new StackPanel();
            sp.Orientation 
= Orientation.Horizontal;
            
if (ig != null)
            {
                sp.Children.Add(ig);
            }
            
else
            {
                
//如果设置图片,显示默认图片
                ig = new Image();
                ig.Source 
= new BitmapImage(new Uri("../Resources/Image/waiting.png", UriKind.RelativeOrAbsolute));
                sp.Children.Add(ig);
            }

            TextBlock tb1 
= new TextBlock();
            tb1.Text 
= Convert.ToString(this.Title);
            sp.Children.Add(tb1);
            cc.Content 
= sp;
        }
    }
}

   通过扩展ChildWindow之后,同样可以实现上图的效果,而且每次都不用复制那段代码。使用时,只要引用此类的命名空间即可,其它的和原来的ChildWindow一样,在XMAL中使用扩展后的ChildWinow代码为

     

代码
<Clu:ExtendChildWindows
    x:Class
="ResourceApp.ChildDetailMetadata" 
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
     xmlns:data
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:Clu
="clr-namespace:ExtendChildWindow;assembly=ExtendChildWindow"
>
    
<Grid>
       

    
</Grid>
</Clu:ExtendChildWindows>

      希望通过上面两个方法来实现带Logo的子窗体,对大家有帮助,大家也可以根据需要选择两种方法的一个。

       如果大家觉得有帮助就帮顶一下,谢谢了

原文地址:https://www.cnblogs.com/888h/p/1865603.html