WPF开发的彩票程序(练手好例子)

C#

浏览数:85

2019-6-8

前言

WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大。

应朋友之邀,编写了一个小程序。程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑。

对于新手学习有很好的借鉴意义,代码已上传到CSDN可以下载。网址见文章底部。

功能说明

   分析彩票的历史记录,根据选取的几个数据,分析记录出现的情况。看下图。

左边是历史记录,右边 是选取的数据 68、 8*。从历史记录查找,符合这个逻辑关系的数据。

技术解析

  我这里分析几个小技术点。

   1)ListView不同行颜色设定

    ListView 有一个属性 ItemContainerStyleSelector,这个属性可以绑定到一个类,你可以在这个类中,根据每行的属性,设定背景。

 1  public class ListViewItemStyleSelector : StyleSelector
 2     {
 3         public override Style SelectStyle(object item,DependencyObject container)
 4         {
 5             Style st = new Style();
 6             st.TargetType = typeof(ListViewItem);
 7             Setter backGroundSetter = new Setter();
 8             backGroundSetter.Property = ListViewItem.BackgroundProperty;
 9             ListView listView =ItemsControl.ItemsControlFromItemContainer(container) as ListView;
10             int index =listView.ItemContainerGenerator.IndexFromContainer(container);
11             if (index % 2 == 0)
12             {
13                 backGroundSetter.Value = Brushes.LightGray;
14             }
15             else
16             {
17                 backGroundSetter.Value = Brushes.White;
18             }
19             st.Setters.Add(backGroundSetter);
20             return st;
21         }
22     }

 2)数字的显示

   ListView 每一列,可以绑定字符串,也可以根据需要绑定控件模版。

 1  <GridView >
 2                             <GridViewColumn Header="序号" Width="50" DisplayMemberBinding="{Binding Path=StrNO}"></GridViewColumn>
 3                             <GridViewColumn Header="日期" Width="120" DisplayMemberBinding="{Binding Path=StrTimeStamp}"></GridViewColumn>
 4                             <GridViewColumn Header="期数" Width="110" DisplayMemberBinding="{Binding Path=StrVolume}"></GridViewColumn>
 5                             <GridViewColumn Header="1列"  Width="55" CellTemplate="{StaticResource ColDigital1}"></GridViewColumn>
 6                             <GridViewColumn Header="2列" Width="55" CellTemplate="{StaticResource ColDigital2}"></GridViewColumn>
 7                             <GridViewColumn Header="3列" Width="55" CellTemplate="{StaticResource ColDigital3}"></GridViewColumn>
 8                             <GridViewColumn Header="4列" Width="55" CellTemplate="{StaticResource ColDigital4}"></GridViewColumn>
 9                             <GridViewColumn Header="5列" Width="55" CellTemplate="{StaticResource ColDigital5}"></GridViewColumn>
10                             <GridViewColumn Header="6列" Width="55" CellTemplate="{StaticResource ColDigital6}"></GridViewColumn>
11                             <GridViewColumn Header="7列" Width="55" CellTemplate="{StaticResource ColDigital7}"></GridViewColumn>
12   </GridView>

   数字是静态绑定到 ColDigital1,ColDigital2…等,咱看看ColDigital1如何实现的。

1  <DataTemplate x:Key="ColDigital1" >
2             <StackPanel Margin="5,2,5,2" HorizontalAlignment="Center" Width="180">
3                 <local:CustomControl_digital x:Name="labelDigital1" Width="30" Height="30" StrDigital="{Binding Path=StrCol1}"></local:CustomControl_digital>
4             </StackPanel>
5 </DataTemplate>

StackPanel 包含了一个自定义控件CustomControl_digital,这个自定控件实现了对数字的绘制。ListView 绑定一个类,这个类有属性字段StrCol1;自定义控件就从StrCol1获取数据绘制。

 1  public class CustomControl_digital : Control
 2     {
 3         static CustomControl_digital()
 4         {
 5             StrDigitalProperty =
 6       DependencyProperty.Register("StrDigital", //属性名称
 7       typeof(string), //属性类型
 8       typeof(CustomControl_digital), //该属性所有者,即将该属性注册到那个类上
 9       new PropertyMetadata("")); //属性默认值
10 
11             DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl_digital), new FrameworkPropertyMetadata(typeof(CustomControl_digital)));
12         }
13 
14         public static readonly DependencyProperty StrDigitalProperty;
15 
16         public static Color defaultColor = Color.FromRgb(41, 57, 85);
17         Color BackColor { get; set; } = defaultColor;
18         public void SetBackColor(int index)
19         {
20             if (index == 0)
21                 BackColor = defaultColor;
22             else
23                 BackColor = Color.FromRgb(62, 175, 14);
24         }
25 
26         public string StrDigital
27         {
28             get { return (string)GetValue(StrDigitalProperty); }
29             set { SetValue(StrDigitalProperty, value); }
30         }
31 
32         protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
33         {
34             base.OnMouseDoubleClick(e);
35         }
36         protected override void OnRender(DrawingContext dc)
37         {
38             base.OnRender(dc);
39             if (StrDigital == "--")
40                 return;
41 
42             double len = Math.Min(ActualHeight, ActualWidth);
43             Point center = new Point(ActualWidth / 2, ActualHeight / 2);
44 
45             Pen pen = new Pen(Brushes.Black, 0);
46             Brush brush = new SolidColorBrush(BackColor);
47 
48             double totalRadius = len / 2;
49             double radius = totalRadius * 9 / 10;
50             dc.DrawEllipse(brush, pen, center, radius, radius);
51 
52             if (!string.IsNullOrEmpty(StrDigital))
53             {
54                 FormattedText text = new FormattedText(StrDigital, CultureInfo.CurrentCulture,
55         FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.White);
56                 Point txtPoint = new Point(center.X - 9, center.Y - 8);
57                 dc.DrawText(text, txtPoint);
58             }
59 
60         }
61     }

技术交流联系qq 13712486

   

作者:源之缘