标签:xmlns namespace void ide system pes csharp tom 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>
标签:xmlns namespace void ide system pes csharp tom banner
原文地址:http://www.cnblogs.com/by-lhc/p/7230003.html