新手求如图所示xaml实现代码,万望高手指点
想要的结果是程序启动时数字背景像图片中的1,2号码一样灰色,当鼠标点击任意号码的时候变成3号码球一样红色并且可以同时选择多个号码球(即为灰色号码球添加鼠标单击事件程序使其号码球变成红底白字号码球),当鼠标再次点击时号码球恢复灰色,谢谢!!
1.自定义UserControl
<UserControl x:Class="WpfApplication1.LotteryBar" 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" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Label x:Name="lbl" Width="{Binding}" Height="{Binding}"> <Label.Template> <ControlTemplate TargetType="Label"> <Grid> <Ellipse StrokeThickness="2"> <Ellipse.Fill> <SolidColorBrush Color="{Binding Path=BackColor,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=UserControl}}"></SolidColorBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter Content="{Binding Path=Text,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=UserControl}}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Label.Template> </Label> </UserControl>
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace WpfApplication1 { /// <summary> /// Interaction logic for LotteryBar.xaml /// </summary> public partial class LotteryBar : UserControl { public LotteryBar() { InitializeComponent(); } public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LotteryBar), new PropertyMetadata(string.Empty)); public Color BackColor { get { return (Color)GetValue(BackColorProperty); } set { SetValue(BackColorProperty, value); } } // Using a DependencyProperty as the backing store for BackColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(Color), typeof(LotteryBar), new PropertyMetadata(Colors.LightGray)); public Color ForeColor { get { return (Color)GetValue(ForeColorProperty); } set { SetValue(ForeColorProperty, value); } } // Using a DependencyProperty as the backing store for ForeColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty ForeColorProperty = DependencyProperty.Register("ForeColor", typeof(Color), typeof(LotteryBar), new PropertyMetadata(Colors.Black,new PropertyChangedCallback(OnForeColorChangedCallback))); public static void OnForeColorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if(e.OldValue != e.NewValue) { var uc = d as LotteryBar; uc.lbl.Foreground = new SolidColorBrush((Color)e.NewValue); } } } }
2.使用UserControl
<Grid Width="200" Height="100"> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <local:LotteryBar Text="01" Width="30" Height="30"></local:LotteryBar> <local:LotteryBar Text="02" Width="30" Height="30" Grid.Column="1"></local:LotteryBar> <local:LotteryBar Text="03" Width="30" Height="30" Grid.Column="2" ForeColor="White" BackColor="Red"></local:LotteryBar> <local:LotteryBar Text="04" Width="30" Height="30" Grid.Column="3"></local:LotteryBar> <local:LotteryBar Text="05" Width="30" Height="30" Grid.Column="4"></local:LotteryBar> <local:LotteryBar Text="06" Width="30" Height="30" Grid.Column="5"></local:LotteryBar> </Grid>
您好!非常感谢你的帮助,我现在想要的结果是程序启动时数字背景像图片中的1,2号码一样灰色,当鼠标点击任意号码的时候变成3号码球一样红色并且可以同时选择多个号码球,当再次点击时恢复到初始灰色状态,弄了好久没成功,求指教,感激不尽,谢谢!!
@梦天涯: 嗯,稍微改下即可
1.自定义UserControl
<UserControl x:Class="WpfApplication1.LotteryBar" 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" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Label x:Name="lbl" Width="{Binding}" Height="{Binding}" MouseDown="lbl_MouseDown"> <Label.Template> <ControlTemplate TargetType="Label"> <Grid> <Ellipse StrokeThickness="2"> <Ellipse.Fill> <SolidColorBrush Color="{Binding Path=BackColor,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=UserControl}}"></SolidColorBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter Content="{Binding Path=Text,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=UserControl}}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Label.Template> </Label> </UserControl>
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace WpfApplication1 { /// <summary> /// Interaction logic for LotteryBar.xaml /// </summary> public partial class LotteryBar : UserControl { public LotteryBar() { InitializeComponent(); } public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LotteryBar), new PropertyMetadata(string.Empty)); public Color BackColor { get { return (Color)GetValue(BackColorProperty); } set { SetValue(BackColorProperty, value); } } // Using a DependencyProperty as the backing store for BackColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(Color), typeof(LotteryBar), new PropertyMetadata(Colors.LightGray)); public Color ForeColor { get { return (Color)GetValue(ForeColorProperty); } set { SetValue(ForeColorProperty, value); } } // Using a DependencyProperty as the backing store for ForeColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty ForeColorProperty = DependencyProperty.Register("ForeColor", typeof(Color), typeof(LotteryBar), new PropertyMetadata(Colors.Black,new PropertyChangedCallback(OnForeColorChangedCallback))); public static void OnForeColorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if(e.OldValue != e.NewValue) { var uc = d as LotteryBar; uc.lbl.Foreground = new SolidColorBrush((Color)e.NewValue); } } public bool Highlight { get { return (bool)GetValue(IsHighlightProperty); } set { SetValue(IsHighlightProperty, value); } } // Using a DependencyProperty as the backing store for IsHighlight. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsHighlightProperty = DependencyProperty.Register("IsHighlight", typeof(bool), typeof(LotteryBar), new PropertyMetadata(false,new PropertyChangedCallback(OnHighlightChangedCallback))); public static void OnHighlightChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.OldValue != e.NewValue) { var uc = d as LotteryBar; var _highlight = (bool)e.NewValue; if (!_highlight) { uc.BackColor = Colors.LightGray; uc.ForeColor = Colors.Black; } else { uc.BackColor = Colors.Red; uc.ForeColor = Colors.White; } } } private void lbl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { if(e.ChangedButton == System.Windows.Input.MouseButton.Left) { Highlight = !Highlight; } } } }
2.使用UserControl
<Grid Width="200" Height="100"> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <local:LotteryBar Text="01" Width="30" Height="30"></local:LotteryBar> <local:LotteryBar Text="02" Width="30" Height="30" Grid.Column="1"></local:LotteryBar> <local:LotteryBar Text="03" Width="30" Height="30" Grid.Column="2" Highlight="True"></local:LotteryBar> <local:LotteryBar Text="04" Width="30" Height="30" Grid.Column="3"></local:LotteryBar> <local:LotteryBar Text="05" Width="30" Height="30" Grid.Column="4"></local:LotteryBar> <local:LotteryBar Text="06" Width="30" Height="30" Grid.Column="5"></local:LotteryBar> </Grid>
@梦天涯:
<Label x:Name="lbl" Width="{Binding}" Height="{Binding}"> <Label.Template> <ControlTemplate TargetType="Label"> <Grid> <Ellipse StrokeThickness="2"> <Ellipse.Fill> <SolidColorBrush Color="{Binding Path=BackColor,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=UserControl}}"></SolidColorBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter Content="{Binding Path=Text,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=UserControl}}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Label.Template> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseUp"> <i:InvokeCommandAction Command="{Binding OnIconButtonClicked}" CommandParameter="{Binding}" /> </i:EventTrigger> </i:Interaction.Triggers> </Label>
label这么些 引用
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
新手的话,放一堆图片就行了。说多了反而麻烦。
@jello chen提供的代码应该可以完美符合你的需求