前言
WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大。
应朋友之邀,编写了一个小程序。程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑。
对于新手学习有很好的借鉴意义,代码已上传到CSDN可以下载。网址见文章底部。
功能说明
分析彩票的历史记录,根据选取的几个数据,分析记录出现的情况。看下图。
左边是历史记录,右边 是选取的数据 68、 8*。从历史记录查找,符合这个逻辑关系的数据。
技术解析
我这里分析几个小技术点。
1)ListView不同行颜色设定
ListView 有一个属性 ItemContainerStyleSelector,这个属性可以绑定到一个类,你可以在这个类中,根据每行的属性,设定背景。
public class ListViewItemStyleSelector : StyleSelector
{
public override Style SelectStyle(object item,DependencyObject container)
{
Style st = new Style();
st.TargetType = typeof(ListViewItem);
Setter backGroundSetter = new Setter();
backGroundSetter.Property = ListViewItem.BackgroundProperty;
ListView listView =ItemsControl.ItemsControlFromItemContainer(container) as ListView;
int index =listView.ItemContainerGenerator.IndexFromContainer(container);
if (index % == )
{
backGroundSetter.Value = Brushes.LightGray;
}
else
{
backGroundSetter.Value = Brushes.White;
}
st.Setters.Add(backGroundSetter);
return st;
}
}
2)数字的显示
ListView 每一列,可以绑定字符串,也可以根据需要绑定控件模版。
<GridView >
<GridViewColumn Header="序号" Width="" DisplayMemberBinding="{Binding Path=StrNO}"></GridViewColumn>
<GridViewColumn Header="日期" Width="" DisplayMemberBinding="{Binding Path=StrTimeStamp}"></GridViewColumn>
<GridViewColumn Header="期数" Width="" DisplayMemberBinding="{Binding Path=StrVolume}"></GridViewColumn>
<GridViewColumn Header="1列" Width="" CellTemplate="{StaticResource ColDigital1}"></GridViewColumn>
<GridViewColumn Header="2列" Width="" CellTemplate="{StaticResource ColDigital2}"></GridViewColumn>
<GridViewColumn Header="3列" Width="" CellTemplate="{StaticResource ColDigital3}"></GridViewColumn>
<GridViewColumn Header="4列" Width="" CellTemplate="{StaticResource ColDigital4}"></GridViewColumn>
<GridViewColumn Header="5列" Width="" CellTemplate="{StaticResource ColDigital5}"></GridViewColumn>
<GridViewColumn Header="6列" Width="" CellTemplate="{StaticResource ColDigital6}"></GridViewColumn>
<GridViewColumn Header="7列" Width="" CellTemplate="{StaticResource ColDigital7}"></GridViewColumn>
</GridView>
数字是静态绑定到 ColDigital1,ColDigital2...等,咱看看ColDigital1如何实现的。
<DataTemplate x:Key="ColDigital1" >
<StackPanel Margin="5,2,5,2" HorizontalAlignment="Center" Width="">
<local:CustomControl_digital x:Name="labelDigital1" Width="" Height="" StrDigital="{Binding Path=StrCol1}"></local:CustomControl_digital>
</StackPanel>
</DataTemplate>
StackPanel 包含了一个自定义控件CustomControl_digital,这个自定控件实现了对数字的绘制。ListView 绑定一个类,这个类有属性字段StrCol1;自定义控件就从StrCol1获取数据绘制。
public class CustomControl_digital : Control
{
static CustomControl_digital()
{
StrDigitalProperty =
DependencyProperty.Register("StrDigital", //属性名称
typeof(string), //属性类型
typeof(CustomControl_digital), //该属性所有者,即将该属性注册到那个类上
new PropertyMetadata("")); //属性默认值 DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl_digital), new FrameworkPropertyMetadata(typeof(CustomControl_digital)));
} public static readonly DependencyProperty StrDigitalProperty; public static Color defaultColor = Color.FromRgb(, , );
Color BackColor { get; set; } = defaultColor;
public void SetBackColor(int index)
{
if (index == )
BackColor = defaultColor;
else
BackColor = Color.FromRgb(, , );
} public string StrDigital
{
get { return (string)GetValue(StrDigitalProperty); }
set { SetValue(StrDigitalProperty, value); }
} protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
}
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
if (StrDigital == "--")
return; double len = Math.Min(ActualHeight, ActualWidth);
Point center = new Point(ActualWidth / , ActualHeight / ); Pen pen = new Pen(Brushes.Black, );
Brush brush = new SolidColorBrush(BackColor); double totalRadius = len / ;
double radius = totalRadius * / ;
dc.DrawEllipse(brush, pen, center, radius, radius); if (!string.IsNullOrEmpty(StrDigital))
{
FormattedText text = new FormattedText(StrDigital, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Verdana"), , Brushes.White);
Point txtPoint = new Point(center.X - , center.Y - );
dc.DrawText(text, txtPoint);
} }
}