Encapsulation of WPF banner controls

Source: Internet
Author: User

First, create the Bannerad user control

A banner user control can contain n Bannerad user controls, where 2 Bannerad user controls are created as examples;

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=" d:designwidth= ">    <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 Interactive logic///    </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=" d:designwidth= ">    <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 Interactive logic///    </summary > Public    Partial Class Bannerad2:usercontrol, Istop    {public        BannerAD2 ()        {            InitializeComponent ();        }        public bool Stopped        {            get            {                return bor. IsMouseOver;}}}}    

Second, create Boolvisibilityconverter class

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 (is Erviceprovider 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 Member public Object Convert (object value, Type targetType, object parameter, System.globalizati On.            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 cultu                    RE) {if (value is Visibility) {if ((Visibility) value = = visibility.visible)                return true;            else return false;        } return false; } #endregion}}

Iii. creating a banner user control

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/mar kup-compatibility/2006 "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "mc:ignorabl E= "D" xmlns:local= "Clr-namespace:wpfapp" d:designheight= "d:designwidth=" > <use                rcontrol.resources> <style targettype= "RadioButton" > <setter property= "Template" > <Setter.Value> <controltemplate targettype= "RadioButton" > &L T grid> <ellipse fill= "Transparent" stroke= "#606e7e" x:name= "Bot" width= "8" height= "8" Vert                        Icalalignment= "Center" horizontalalignment= "center" ></Ellipse> </Grid> <controltEmplate. triggers> <trigger property= "IsChecked" value= "True" > & Lt                        Setter targetname= "Bot" property= "Fill" value= "#606e7e" ></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </sett Er. value> </Setter> </Style> </UserControl.Resources> <Grid> <gr ID x:name= "Adgrid" > <local:bannerad1 visibility= "{Binding Elementname=btn1,path=ischecked,mode=oneway,co Nverter={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= "RadioB Utton_mouseenter "ischecked=" True "></RadioButton> <radiobutton x:name=" Btn1 "margin=" 5,0,5,0 "Mouse Enter= "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 Interactive logic </summary> public partial class Banner:usercontrol {list<radiobutton> RadioButtons = new L        Ist<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.buttonp Anel. Children) {RadioButton rbtn = Ele as RadioButton;            if (rbtn! = null) Radiobuttons.add (RBTN); } foreach (Var ele in).                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 =&G T            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;} }}

Iv. using the banner user control

Create a window form to add namespaces (xmlns:local= "Clr-namespace:wpfapp") and Banner user controls (<local:banner></local) to the form's XAML file: 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=" width= "525" >    <Grid>        <Grid.RowDefinitions>            <rowdefinition Height= "" "></RowDefinition>            <rowdefinition height=" * * "></RowDefinition>        </ grid.rowdefinitions>        <local:Banner></local:Banner>    </Grid></Window>

  

Encapsulation of WPF banner controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.