How to read excel using Infragistics WPF - c#

I have excel where in Sheet 1 i have 2 tables ,i want to read data by infragistics WPF and bind in UI below is the structure of data in excel , i am new to WPF can anyone help

One of the methods to read the Excel file is using the XamSpreadsheet control that allows visualizing and editing of spreadsheet data.
There is the code example.
using Infragistics.Documents.Excel;
using System;
using System.IO;
using System.Windows;
using System.Windows.Resources;
namespace ReadExcelFile
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadFile();
}
private void LoadFile()
{
// For this example the excel document is located in resources
var fileName = "/ReadExcelFile;component/Resources/Book1.xlsx";
Stream stream = GetFileAsStream(fileName);
if (Workbook.Load(stream) is Workbook wb)
{
this.xamSpreadsheet1.Workbook = wb;
this.Title = "Book1.xlsx";
}
}
public static Stream GetFileAsStream(string fullName)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri(fullName, UriKind.Relative));
return sri.Stream;
}
}
}
The XAML:
<Window x:Class="ReadExcelFile.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ig="http://schemas.infragistics.com/xaml"
mc:Ignorable="d"
Height="450" Width="800">
<Grid>
<ig:XamSpreadsheet x:Name="xamSpreadsheet1" />
</Grid>
</Window>
The application will looks like on the screenshot:
It is necessary add references to the following assemblies:
InfragisticsWPF4.Controls.Grids.XamSpreadsheet
InfragisticsWPF4.Documents.Excel
For additional information see: XamSpreadsheet

Related

UWP: use 1 MediaSource for several Players

I have some uwp application, where several videos are played simultaneously. I thought it might be a good idea to use 1 media source to improve performance. But I'm not sure why this idea doesn't work.
MainPage.xaml:
<Page
x:Class="UWP_OneMediaSourceForSeveralPlayers.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP_OneMediaSourceForSeveralPlayers"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
Loaded="Page_Loaded">
<StackPanel Orientation="Vertical" Background="Yellow" Height="400" Width="200">
<MediaPlayerElement x:Name="Player1" Height="200" Width="200" />
<MediaPlayerElement x:Name="Player2" Height="200" Width="200" />
</StackPanel>
</Page>
MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace UWP_OneMediaSourceForSeveralPlayers
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var uri = new Uri(BaseUri, "/Assets/Videos/video.mp4");
var mediaSource = MediaSource.CreateFromUri(uri);
Player1.Source = mediaSource;
Player2.Source = mediaSource;
Player1.MediaPlayer.Play();
Player2.MediaPlayer.Play();
}
}
}
That's what I see:
So, looks like the first video is loaded. But not the second...
NOW question: why I can not use 1 media source for 2 players? How to make it work? Or do you have any other idea how to run the same file in several players?
P.S. creating 2 media sources from URI is not a good solution for me, because my app can have a lot (10+) videos running at the same time. If I create a media source for each player it will hurt performance.
So what you can actually do here is share a MediaPlayer between the two MediaPlayerElement instance.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var uri = new Uri(BaseUri, "/Assets/Videos/video.mp4");
var mediaSource = MediaSource.CreateFromUri(uri);
var player = new MediaPlayer
{
Source = new MediaPlaybackItem(mediaSource)
};
Player1.SetMediaPlayer(player);
Player2.SetMediaPlayer(player);
player.Play();
}
If you don't require the transport controls from MediaPlayerElement, it's probably better to use MediaPlayerPresenter instead.

Using open source code LiveCharts to get a scatter plot that is created programmatically but I can't get a plot

I'm trying to use open source code LiveCharts to get a scatter plot that is created programmatically but I get an error object reference not set to an object.
It is aborting in LiveCharts when I "show" the window. I'm new to LiveCharts and particularly the scatter plot. I can generate the scatter plot example that relies on the XAML file but I want more flexibility to plot a variety of graphs programatically.
I'm trying to use open source code LiveCharts to get a scatter plot that is created programmatically but I get an error object reference not set to an object.
using System;
using System.Collections.Generic;
using System.Windows;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using LiveCharts.Charts;
namespace TestScatterPlot01
{
public partial class MainWindow : Window
{
public ChartValues<ObservablePoint> ValuesA { get; set; }
public MainWindow()
{
InitializeComponent();
ValuesA = new ChartValues<ObservablePoint>();
for(var i = 0; i < 5; i++)
{
double x = (double)i;
double y = (double)(i * i);
ValuesA.Add(new ObservablePoint(x, y));
}
CartesianChart theChart = new CartesianChart();
theChart.LegendLocation = LegendLocation.Bottom;
ScatterSeries s1 = new ScatterSeries();
s1.Title = "Series A";
s1.Values = ValuesA;
s1.PointGeometry = DefaultGeometries.Diamond;
theChart.AddToView(s1);
Content = theChart;
this.Show();
}
}
}
XAML file (I had to reconstruct this as TestScatterPlot03. The .cs file now has the namespace TestScatterPlot03).
<Window x:Class="TestScatterPlot03.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestScatterPlot03"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>

Using InvokeMember with WPF WebBrowser HTMLElement throws exception

I'm trying to simulate a user typing a value into an "input" field on a website, and I'm getting very strange exception when trying to run InvokeMember. I tested several approaches and I found that most online documentation is outdated and simply doesn't work. Unfortunately the mostly suggested solution to my problem seems to throw a very strange exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
''System.MarshalByRefObject.InvokeMember(string,
System.Reflection.BindingFlags, System.Reflection.Binder, object[],
System.Reflection.ParameterModifier[],
System.Globalization.CultureInfo, string[])' is inaccessible due to
its protection level'
This is an example code that recreates the problem:
MainWindow.xaml
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Example"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<WebBrowser x:Name="ie"/>
</DockPanel>
</Window>
MainWindow.xaml.cs
using mshtml;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Navigation;
namespace Example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ie.LoadCompleted += (o, e) =>
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (wo, we) => { Thread.Sleep(5000); };
worker.RunWorkerCompleted += (wo, we) => { DelayedLoadCompleted(); };
worker.RunWorkerAsync();
};
ie.Navigate(#"https://google.com");
}
private void DelayedLoadCompleted()
{
HTMLDocument doc = (ie.Document as HTMLDocument);
dynamic input = doc.getElementById("lst-ib");
input.InvokeMember("onkeypress", new { Key = 65 }); //throws that excetpion
input.InvokeMember("click"); //throws that excetpion
input.click(); //works fine
}
}
}
The line input.InvokeMember("click"); throws that exception, but input.click(); works just fine.
What is the cause of that exception?

Embedding XpsDocument PowerPoint slide shows into a WPF DocumentViewer

I've recently had a go at embedding a PowerPoint file as an XpsDocument in WPF.
It is a simple WPF application in which I embed a DocumentViewer property into my MainWindow.xaml grid:
<Window x:Class="PowerPoint2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PowerPoint2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DocumentViewer
Name="DocumentviewPowerPoint"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
</Grid>
To create the document bound to "DocumentviewPowerPoint" I convert the PowerPoint file that has been opened into Xps format and bind this variable to the XAML property mentioned earlier:
using System;
using System.IO;
using System.Windows;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using Application = Microsoft.Office.Interop.PowerPoint.Application;
namespace PowerPoint2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
const string powerPointFile = #"c:\temp\ppt.pptx";
var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".pptx";
var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);
DocumentviewPowerPoint.Document = xpsDocument.GetFixedDocumentSequence();
}
private static XpsDocument ConvertPowerPointToXps(string pptFilename, string xpsFilename)
{
var pptApp = new Application();
var presentation = pptApp.Presentations.Open(pptFilename, MsoTriState.msoTrue, MsoTriState.msoFalse,
MsoTriState.msoFalse);
try
{
presentation.ExportAsFixedFormat(xpsFilename, PpFixedFormatType.ppFixedFormatTypeXPS);
}
catch (Exception ex)
{
MessageBox.Show("Failed to export to XPS format: " + ex);
}
finally
{
presentation.Close();
pptApp.Quit();
}
return new XpsDocument(xpsFilename, FileAccess.Read);
}
}
}
This all works well enough when running the program, showing the Xps document embedded into the WPF:
My question is how can I further modify my code in order to display the PowerPoint not just as a series of scrollable slides as shown, but as an actual slide show? I would like to make further updates to enable the user to navigate to the following slide on each mouse click - like a 'proper' presentation. My problem is that I am unfamiliar with the usage of the XpsDocument Apis - I don't know if it's a case of using these to achieve what I want or is it in the settings properties of the presentation variable that gets converted to the Xps format.
I managed to solve the particular problem I was interested in.
Please refer to this blog posting for a detailed explanation:
Controlling DocumentViewer methods and properties using MVVM
The solution addresses the problem of being able to enable individual PowerPoint slides (converted to xps file format) to occupy the WHOLE of the available windows space by invoking the relevant combination of DocumentViewer methods.
On pressing the screen button to invoke the RelayCommand, the following combination of DocumentViewer API calls in the MainWindowViewModel.cs class was observed to work:
public ICommand Command
{
get
{
return _command ?? (_command = new RelayCommand(
x =>
{
DocumentViewer = MainWindow.GetInstance();
const string powerPointFile = #"c:\temp\ppt.pptx";
var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".xps";
var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);
FixedFixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
DocumentViewer.Document = FixedFixedDocumentSequence;
DocumentViewer.GoToPage(1);
DocumentViewer.FitToMaxPagesAcross(1);
WindowState = WindowState.Maximized;
DocumentViewer.FitToMaxPagesAcross(1);
}));
}
}
And to obtain the DocumentViewer instance itself? I also need to update the MainWindow.xaml.cs to get it to return the instance of the DocumentViewer object:
using System.Windows.Controls;
namespace DocumentView
{
public partial class MainWindow
{
private static DocumentViewer _docViewer;
public MainWindow()
{
InitializeComponent();
_docViewer = DocumentViewPowerPoint;
}
public static DocumentViewer GetInstance()
{
return _docViewer;
}
}
}
Where DocumentViewPowerPoint is the name given to the DocumentViewer in the MainWindow.xaml:
<Window x:Class="DocumentView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DocumentView"
mc:Ignorable="d"
WindowState="{Binding WindowState, Mode=TwoWay}"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<DocumentViewer
Grid.Row="0"
Document="{Binding FixedFixedDocumentSequence}"
Name="DocumentViewPowerPoint"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
<Button
Grid.Row="1"
Command="{Binding Command}"
Width="70" Height="30" Content="Press" />
</Grid>

Adding models/content to the HelixViewport3D in Helix 3D Toolkit

I am trying to load and display a 3d model in the HelixViewport3D.
I can get as far as loading the model (OBJ), but I cannot understand how to get the model into the viewport.
Here's a screenshot of my WPF form...
The viewprot is named as 'myView' - I thought I could hook into that to add my model, but I don't see anything obvious to use.
Here's my XAML of the form :
<Window x:Class="HelixTrial.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
<HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
<!-- Remember to add light to the scene -->
<HelixToolkit:SunLight/>
<!-- You can also add elements here in the xaml -->
<HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
</HelixToolkit:HelixViewport3D>
</Grid>
</Grid>
And here is the code for my form.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Media.Media3D;
using HelixToolkit.Wpf;
namespace HelixTrial
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObjReader CurrentHelixObjReader = new ObjReader();
Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");
// Now how to load it into the viewport... ?
}
}
}
You can see where I am stuck. Could someone help get me on track please.
After some experimenting I found the solution.
I added the following to my XAML :
<ModelVisual3D x:Name="foo"/>
The trick was to give it a name, ie 'foo' for example. The XAML will now look like this :
<Window x:Class="HelixTrial.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
<HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
<!-- Remember to add light to the scene -->
<HelixToolkit:SunLight/>
<ModelVisual3D x:Name="foo"/>
<!-- You can also add elements here in the xaml -->
<HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
</HelixToolkit:HelixViewport3D>
</Grid>
</Grid>
Then in the code (as per what I posted in my original question above) you can do this :
ObjReader CurrentHelixObjReader = new ObjReader();
Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");
// Display the model
foo.Content = MyModel;
Easy when you find out how ;)
# This is my code which works fine #
<Window x:Class="Helix_Car_Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Helix_Car_Demo"
xmlns:helix="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Background="Black"
WindowState="Maximized" WindowStyle="None"
Title="MainWindow" Height="350" Width="525">
<Grid>
<helix:HelixViewport3D Name="viewport3D" ZoomExtentsWhenLoaded="True" MouseDoubleClick="viewport3D_MouseDoubleClick">
<helix:SunLight/>
</helix:HelixViewport3D>
</Grid>
Here is my .cs File
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ModelVisual3D device = new ModelVisual3D();
device.Content = getModel("hearse.3ds");
viewport3D.Children.Add(device);
}
public Model3D getModel(string path)
{
Model3D device = null;
try
{
viewport3D.RotateGesture = new MouseGesture(MouseAction.LeftClick);
ModelImporter import = new ModelImporter();
device = import.Load(path);
}
catch (Exception e)
{ }
return device;
}
}
}

Categories