背景 winform切为wpf,使用devexpress插件
参考的网站为:https://docs.devexpress.com/WPF/7875/wpf-controls
1.GridControl行号设置
这里主要有两个问题,一个是行号要从1开始,二是行号这一列的行宽设置(默认行宽到100后,数字就显示不全了)
行宽是IndicatorWidth 属性,行号是RowIndicatorContentTemplate 实现
<dxg:TableView ShowGroupPanel="False" Name="gvTable" IndicatorWidth="50" RowIndicatorContentTemplate="{StaticResource rowIndicatorContentTemplate}"/>
<DataTemplate x:Key="rowIndicatorContentTemplate">
<StackPanel VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=RowHandle.Value,Converter={StaticResource rowIndicatorConverter}}"
TextAlignment="Center"
Foreground="Black"/>
</StackPanel>
</DataTemplate>
public class RowIndicatorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (value == null)
{
return 0;
}
int num = (int) value;
num = num + 1;
return num;
}
catch (Exception ex)
{
return 0;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
}
}
2.ChartControl 使用
这块wpf和winform的实现还是有点区别的。
legend是图例,通过Orientation和VericalPosition来控制位置。AxisX和AxisY为X和Y轴。
<dxc:ChartControl x:Name="chartControl1" Grid.Column="1" Grid.Row="1">
<dxc:ChartControl.Legend>
<dxc:Legend HorizontalPosition="Center" Orientation="Horizontal" VerticalPosition="TopOutside" FontSize="10" Background="White" Name="chartLegend"/>
</dxc:ChartControl.Legend>
<dxc:XYDiagram2D x:Name="xychart" EnableAxisXNavigation="True" EnableAxisYNavigation="True">
<dxc:XYDiagram2D.ContextMenu>
<ContextMenu>
<MenuItem Name="miCopy" Header="拷贝到剪贴板" Click="miCopy_Click" />
</ContextMenu>
</dxc:XYDiagram2D.ContextMenu>
<dxc:XYDiagram2D.AxisX>
<dxc:AxisX2D>
<dxc:AxisX2D.Title >
<dxc:AxisTitle/>
</dxc:AxisX2D.Title>
</dxc:AxisX2D>
</dxc:XYDiagram2D.AxisX>
<dxc:XYDiagram2D.AxisY>
<dxc:AxisY2D>
<dxc:AxisY2D.Title >
<dxc:AxisTitle/>
</dxc:AxisY2D.Title>
<dxc:AxisY2D.VisualRange>
<dxc:Range MaxValue="100" MinValue="0"></dxc:Range>
</dxc:AxisY2D.VisualRange>
<dxc:AxisY2D.WholeRange>
<dxc:Range MaxValue="100" MinValue="0"></dxc:Range>
</dxc:AxisY2D.WholeRange>
</dxc:AxisY2D>
</dxc:XYDiagram2D.AxisY>
</dxc:XYDiagram2D>
</dxc:ChartControl>
添加数据内容,可以为点(PointSeries2D),曲线(SplineSeries2D),或者饼状图(PieSeries2D)等
PointSeries2D series1 = new PointSeries2D();
series1.DisplayName = featureType;
series1.MarkerModel = new CircleMarker2DModel();
series1.Brush = Brushes.Red;
series1.MarkerSize = 5;
鼠标滚动来放大缩小图片是通过EnableAxisXNavigation和EnableAxisYNavigation,但是默认为true的话初始化后就有滚动条很难看,所以改为当滚轮滚动时,设置这两个值。
private void xychart_MouseWheel(object sender, MouseWheelEventArgs e)
{
xychart.EnableAxisXNavigation = true;
xychart.EnableAxisYNavigation = true;
}
图片右键复制
private void miCopy_Click(object sender, RoutedEventArgs e)
{
MemoryStream stream = new MemoryStream();
chartControl1.ExportToImage(stream);
System.Windows.Forms.Clipboard.Clear();
System.Windows.Forms.Clipboard.SetImage(System.Drawing.Image.FromStream(stream));
}
3.GridView.GetDataRow的替换
winform中为 DataRow drFocused = gvHistory.GetDataRow(gvHistory.FocusedRowHandle);
wpf替换为
DataRowView obj = gdXls.GetRow(firstRow - 1) as DataRowView;
DataRow fieldRow = obj.Row;
4.DocumentManager的替换
wpf中用DockLayoutManager 替换同时手动计算每个子窗口的位置(以三行两列为例)
<dxdo:DockLayoutManager Margin="12" Name="dockManager1">
<dxdo:LayoutGroup Orientation="Horizontal">
<dxdo:DocumentGroup x:Name="documentGroup" DockItemInterval="10" MDIStyle="MDI" Orientation="Vertical">
</dxdo:DocumentGroup>
</dxdo:LayoutGroup>
</dxdo:DockLayoutManager>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
documentGroup.Items.Add(CreateDocumentPanel(i,j));
}
}
}
private DocumentPanel CreateDocumentPanel(int i, int j) {
double width = documentGroup.ActualWidth / 2;
double height = documentGroup.ActualHeight / 3;
DocumentPanel panel = new DocumentPanel();
panel.Name = "panel" + i + j;
ucChart control=null;
if (j == 0) {
if (i == 0) {
control = new ucChart(0);
}
else if (i == 1)
{
control = new ucChart(3);
}
else if (i == 2)
{
control = new ucChart(5);
}
}
else if (j == 1)
{
if (i == 0)
{
control = new ucChart(2);
}
else if (i == 1)
{
control = new ucChart(1);
}
else if (i == 2)
{
control = new ucChart(4);
}
}
if (control != null)
{
panel.Content = control;
}
panel.MDISize = new Size(width,height);
panel.MDILocation = new Point(j*width,i*height);
return panel;
}
}
5.ImageList图片导出
winfrom中使用ImageList来存放资源,wpf切换时,需要把这些图片导出为png。
ImageList imageList = new ImageList();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("img.ImageStream")));
int i = 0;
foreach (Image img in imageList.Images)
{
img.Save(i+++".png",ImageFormat.Png);
}
来源:CSDN
作者:bornonew
链接:https://blog.csdn.net/bornonew/article/details/103867944