wpf banner控件的封装

一、创建BannerAD用户控件

一个Banner用户控件可以包含n个BannerAD用户控件,在此创建2个BannerAD用户控件作为示例;

BannerAD1.xaml

<UserControl x:Class="WpfApp.BannerAD1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Border Name="bor">
        <Border.Background>
            <ImageBrush ImageSource="/images/test1.jpg" Stretch="Fill" RenderOptions.BitmapScalingMode="Fant"></ImageBrush>
        </Border.Background>
    </Border>
</UserControl>

BannerAD1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp
{
    /// <summary>
    /// BannerAD1.xaml 的交互逻辑
    /// </summary>
    public partial class BannerAD1 : UserControl, IStop
    {
        public BannerAD1()
        {
            InitializeComponent();
        }

        public bool Stopped
        {
            get
            {
                return bor.IsMouseOver;
            }
        }
    }
}

BannerAD2.xaml

<UserControl x:Class="WpfApp.BannerAD2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Border Name="bor">
        <Border.Background>
            <ImageBrush ImageSource="/images/test2.jpg" Stretch="Fill" RenderOptions.BitmapScalingMode="Fant"></ImageBrush>
        </Border.Background>
    </Border>
</UserControl>

BannerAD2.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp
{
    /// <summary>
    /// BannerAD2.xaml 的交互逻辑
    /// </summary>
    public partial class BannerAD2 : UserControl, IStop
    {
        public BannerAD2()
        {
            InitializeComponent();
        }

        public bool Stopped
        {
            get
            {
                return bor.IsMouseOver;
            }
        }
    }
}

二、创建BoolVisibilityConverter类

BoolVisibilityConverter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfApp
{
    [ValueConversion(typeof(bool), typeof(Visibility))]
    class BoolVisibilityConverter : MarkupExtension, IValueConverter
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new BoolVisibilityConverter { FromType = this.FromType ?? typeof(bool), TargetType = this.TargetType ?? typeof(Visibility), Parameter = this.Parameter };
        }

        public object Parameter { get; set; }
        public Type TargetType { get; set; }
        public Type FromType { get; set; }

        #region IValueConverter成员
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility result = Visibility.Hidden;
            if (value is bool)
            {
                if ((bool)value)
                    result = Visibility.Visible;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility)
            {
                if ((Visibility)value == Visibility.Visible)
                    return true;
                else
                    return false;
            }
            return false;
        }
        #endregion
    }
}

三、创建Banner用户控件

Banner.xaml

<UserControl x:Class="WpfApp.Banner"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             xmlns:local="clr-namespace:WpfApp"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="RadioButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <Grid>
                            <Ellipse Fill="Transparent" Stroke="#606e7e" x:Name="Bot" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"></Ellipse>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="Bot" Property="Fill" Value="#606e7e"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid  x:Name="ADGrid">
            <local:BannerAD1 Visibility="{Binding ElementName=Btn1,Path=IsChecked,Mode=OneWay,Converter={local:BoolVisibilityConverter}}"></local:BannerAD1>
            <local:BannerAD2 Visibility="{Binding ElementName=Btn2,Path=IsChecked,Mode=OneWay,Converter={local:BoolVisibilityConverter}}"></local:BannerAD2>
        </Grid>
        <StackPanel x:Name="buttonPanel" Panel.ZIndex="1" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,10" Orientation="Horizontal">
            <RadioButton x:Name="Btn2" Margin="5,0,5,0" MouseEnter="RadioButton_MouseEnter" IsChecked="True"></RadioButton>
            <RadioButton x:Name="Btn1" Margin="5,0,5,0" MouseEnter="RadioButton_MouseEnter"></RadioButton>
        </StackPanel>
    </Grid>
</UserControl>

Banner.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp
{
    /// <summary>
    /// Banner.xaml 的交互逻辑
    /// </summary>
    public partial class Banner : UserControl
    {
        List<RadioButton> radioButtons = new List<RadioButton>();
        List<IStop> iStops = new List<IStop>();
        DispatcherTimer timer = new DispatcherTimer();

        public Banner()
        {
            InitializeComponent();
            this.Loaded += Banner_Loaded;
        }

        private void Banner_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var ele in this.buttonPanel.Children)
            {
                RadioButton rbtn = ele as RadioButton;
                if (rbtn != null)
                    radioButtons.Add(rbtn);
            }
            foreach (var ele in this.ADGrid.Children)
            {
                IStop iStop = ele as IStop;
                if (iStop != null)
                    iStops.Add(iStop);
            }
            if (radioButtons.Count > 1)
            {
                timer.Interval = new TimeSpan(0, 0, 5);
                timer.Tick += Timer_Tick;
                timer.Start();
            }

        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (iStops.Any(c => c.Stopped))
                return;
            int currentIndex = 0;
            for (int i = 0; i < this.radioButtons.Count; i++)
            {
                if (this.radioButtons[i].IsChecked == true)
                {
                    if (this.radioButtons[i].IsMouseOver)
                        currentIndex = -1;
                    else
                        currentIndex = i;
                    break;
                }
            }
            if (currentIndex != -1)
            {
                while (true)
                {
                    currentIndex++;
                    if (currentIndex == this.radioButtons.Count)
                        currentIndex = 0;
                    if (this.radioButtons[currentIndex].Visibility == Visibility.Visible)
                    {
                        this.radioButtons[currentIndex].IsChecked = true;
                        break;
                    }
                }
            }
        }

        private void RadioButton_MouseEnter(object sender, MouseEventArgs e)
        {
            RadioButton rbtn = sender as RadioButton;
            rbtn.IsChecked = true;
        }
    }

    public interface IStop
    {
        bool Stopped { get; }
    }
}

四、使用Banner用户控件

创建一个Window窗体,在窗体的xaml文件中添加命名空间(xmlns:local="clr-namespace:WpfApp")和Banner用户控件(<local:Banner></local:Banner>);

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="4*"></RowDefinition>
            <RowDefinition Height="3*"></RowDefinition>
        </Grid.RowDefinitions>
        <local:Banner></local:Banner>
    </Grid>
</Window>

  

时间: 2024-10-11 12:34:38

wpf banner控件的封装的相关文章

wpf 修改控件Background

以TextBox 控件为例 一  Brushes.颜色 textBoxName.Background = Brushes.Blue; 二 背景色值#FFD2D2D2 1 .textBoxName.Background=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD2D2D2")); 2.textBoxName.Background = new SolidColorBrush(Colors.White);

WPF获取控件内部的ScrollViewer,并控制ScrollViewer操作

//获取内部  ScrollViewer方法 public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject        {            if (obj != null)            {                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)          

WPF 布局控件 之 DockPanel

DockPanel为容器控件 主要了解其Dock属性和LastChildFill属性的使用 一.LastChildFill="True" 时 代码: <DockPanel LastChildFill="True"> <Button DockPanel.Dock="Top">Top</Button> <Button DockPanel.Dock="Bottom">Bottom<

WPF条形码控件支持大多数流行的一维和二维条形码Barcode Professional

Barcode Professional for WPF是一款轻量级的 .NET 程序集,为你的WPF程序生成高质量的基于矢量的条码控件,支持大多数流行的一维和二维条形码:Code 39, Code 128, GS1-128, GS1 DataBar (RSS-14),  EAN 13 & UPC, Postal (USPS, British Royal Mail, Australia Post, DHL, etc.), Data Matrix, QR Code, PDF 417, UPS Ma

WPF Popup 控件导致被遮挡内容不刷新的原因

WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比如最大最小化一下窗口),就连网上传说的这个方法也不行 ? 1 2 3 4 5 6 7 8 9 10 public static class UiHelper {     private delegate void NoArgDelegate();     public static void Ref

iOS开发之资讯类App常用分类控件的封装与实现(CollectionView+Swift3.0+)

今天博客中,我们就来实现一下一些常用资讯类App中常用的分类选择的控件的封装.本篇博客中没有使用到什么新的技术点,如果非得说用到了什么新的技术点的话,那么勉强的说,用到了一些iOS9以后UICollectionView添加的一些新的特性.本篇博客所涉及的技术点主要有UICollectionView的Cell移动,手势识别,控件封装,闭包回调,面向接口编程,Swift中的泛型等等.这些技术点在之前的博客中也多次使用到,只不过本篇博客使用这些技术点来完成我们的具体需求. 一.实例运行效果 先入为主,

WPF布局控件常用属性介绍

WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0) 概述:WPF布局控件都是派生自System.Windows.Controls.Panel抽象类的面板,Panel类继承自 FrameworkElement,Panel类本身并没有什么特别的,但是WPF中提供了许多用于布局的控件都继承自Panel类,如 StackPanel控件,WrapPanel,DockPanel,Grid,UniformGrid,Canva

WPF常用控件总结及其应用demo

WPF常用控件总结及其应用 一.控件 1.WrapPanel布局控件:可以实现当空间不足时子控件自动往下一行布局,空间充足时又会自动调整行布局.常用布局控件还有StackPanel(设置其子元素是垂直排列还是水平排列).Grid(通过定义行和列来绘制出一个表格).Canvas(通过指定相对于其的坐标来指定子控件的位置).DockPanel(设置其子元素如何停靠,DockPanel.Left.DockPanel.Right.DockPanel.Top.DockPanel.Bottom). 2.Sc

wpf 保存控件中的内容为图片格式

黄色的是wpf控件的名称! //保存到特定路径            FileStream fs = new FileStream(@"C:\image.png", FileMode.Create);            //对象转换成位图            RenderTargetBitmap bmp = new RenderTargetBitmap((int)this.mediaElement1.ActualWidth, (int)this.mediaElement1.Act