DataGrid TSV file with dynamic columns - c#

I am trying to create a DataGrid in WPF, in order to make an editor for some tab separated files. The first line of the TSV contains the columns. Also after some research, I found this code
Import data from text file and display in datagrid
which works pretty good, but, when the file is loaded the window becomes unresponsive. Or when I try to resize the window (or scroll down) again the same, it takes time to load it
This is from MainWindow.xaml
<DataGrid x:Name="dtGrid" Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding}"/>
Mainwindow.cs
dtGrid.DataContext = Helper.DataTableFromTextFile("tsv.txt", '\t');
PS. I also tried this too.
dtGrid.ItemsSource = Helper.DataTableFromTextFile("tsv.txt", '\t').DefaultView;
Any ideas?
EDIT:
<Window x:Class="MyFileEdit.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:MyFileEdit"
mc:Ignorable="d"
Title="File Edit" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="16*" MinHeight="16" MaxHeight="16"/>
<RowDefinition Height="303*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="dtGrid" Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding}"/>
</Grid>
</Window>

Related

Merge xaml files using resource dictionaries

The information on merging two XAML files is kind of confusing, could someone point me in the right direction? (W10, VS2017, UAP, Midi)
I have app.xaml, MainPage.xaml, and A.xaml. I also have an xaml resource dictionary. I want to insert A.xaml into MainPage.xaml.
(A is actually called MidiWrapper.xaml)
app.xaml
<Application
x:Class="Kawai_ES110_Midi.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
Following How can i combine multiple XAML files using C# in WPF? I get access violation when I put first solution into app.xaml
MainPage.xaml
<Page
x:Class="Kawai_ES110_Midi.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kawai_ES110_Midi"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="XAMLWrapper.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
</Grid>
</Page>
A.xaml (aka MidiWrapper.xaml)
<ResourceDictionary
x:Class="Kawai_ES110_Midi.MidiWrapper"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kawai_ES110_Midi"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListBox x:Name="midiInPortListBox"/>
<ListBox x:Name="midiOutPortListBox"/>
</ResourceDictionary>
Resource Dictionary xaml aka (XAMLWrapper.xaml)
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
Thanks for your help in advance. Maybe app.xaml needs reference the resource dictionary and/or put "merged" tags into MainPage and A. Or perhaps MainPage and A have to also be/act like dictionaries?
I've updated my code, but I don't see the listboxes showing up still
I'm not sure that what you are trying to do is possible using a resource dictionary. A resource dictionary is exactly that, a list of things that are resources for your XAML so you might define styles in there or templates, etc. These are then referenced by your XAML that uses the resource. I've never tried to define elements in one but I don't think you can initialise a grid with it (which is what I think you are trying to do?).
Edit:
Re-reading your original post in combination with the comment i don't think you want resource dictionaries for this at all. What you want is a separate user control. You can find information on this here but as a short example i created a WPF project and added 2 user controls, 1 that contains a list and one that contains a button.
Mainwindow.xaml
<Window x:Class="test.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:test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:ListUserControl Grid.Column="0" Grid.Row="0"/>
<local:ButtonUserControl Grid.Column="0" Grid.Row="1"/>
</Grid>
</Window>
ListUserControl.xaml
<UserControl x:Class="test.ListUserControl"
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:test"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="White">
<StackPanel>
<ListBox>
<ListBoxItem>ListBox Item #1</ListBoxItem>
<ListBoxItem>ListBox Item #2</ListBoxItem>
<ListBoxItem>ListBox Item #3</ListBoxItem>
</ListBox>
</StackPanel>
</UserControl>
ButtonUserControl.xaml
<UserControl x:Class="test.ButtonUserControl"
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:test"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="DarkRed">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,10,10" VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>
This achieves the separation between the elements that i think you are trying to achieve. So this should allow you to compose a view of several user controls where each control is a collection of controls (potentially other user controls but this can get messy). This could also be achieved using data templates as described here where you can see the content is being changed based on which template is applicable. You could define the templates in your resource dictionary and reference them that way. I think this could also be worth referencing as whilst not directly related to your question it outlines the various ways you can segment content within your application and has some relevant discussion.

WPF C# databinding

I'm trying to go along tutorial from https://msdn.microsoft.com/en-us/data/gg610409.aspx and https://msdn.microsoft.com/en-us/data/gg638943, but my Data Sources are empty when it should automatically get tables from entity data model. I must add Data Sources manualy. What is more, in tutorial there are automatically added methods like "GetCustomersQuery" after drag'n'drop items from Data Sources, but I have only this:
WpfApplication4.DatabaseFCDataSet databaseFCDataSet = ((WpfApplication4.DatabaseFCDataSet)(this.FindResource("databaseFCDataSet")));
// Load data into the table Person. You can modify this code as needed.
WpfApplication4.DatabaseFCDataSetTableAdapters.PersonTableAdapter databaseFCDataSetPersonTableAdapter = new WpfApplication4.DatabaseFCDataSetTableAdapters.PersonTableAdapter();
databaseFCDataSetPersonTableAdapter.Fill(databaseFCDataSet.Person);
System.Windows.Data.CollectionViewSource personViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personViewSource")));
personViewSource.View.MoveCurrentToFirst();
This isn't so clean and simple as in tutorial. Am I doing something wrong? VS version 2012 and 2015.
Binding to the controls (List Box) is done in xaml automatically after drag'n'drop table from Data Source:
<Window x:Class="WpfApplication4.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:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<local:DatabaseFCDataSet x:Key="databaseFCDataSet"/>
<CollectionViewSource x:Key="personViewSource" Source="{Binding Person, Source={StaticResource databaseFCDataSet}}"/>
</Window.Resources>
<Grid DataContext="{StaticResource personViewSource}">
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="211" Margin="36,33,0,0" VerticalAlignment="Top" Width="152" DisplayMemberPath="Name" ItemsSource="{Binding}" SelectedValuePath="IdPerson"/>
</Grid>

Get XAML code in WPF

How can I get XAML code of a window as a string, change this string, and upload this string as a xaml-code of window?
For example, I have
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="0,0,9,38" Name="button1" Height="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="132"></ComboBox>
</Grid>
</Window>
and I want to have this xaml code in the end
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="0,0,9,38" Name="button1" Height="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="132">
<ComboBox.ItemTemplate>
<DataTemplate>
<local:d_dddw_imp_insurance_list />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
If your using wpf/xaml with c#, visual studio generally compile the xaml code to c#, to be as performant as possible. If you want to have some dynamic XAML, you need to use XamlReader to read xaml and display at "on your own".
Just take a look at: Loading XAML XML through runtime
Than you can change or manipulate the xaml and display the result very easily.

WPF put a group box in the middle or inside a user control

I got a user control named Palabok:
<UserControl x:Class="NeocClinic.WPFSystem.Templatas.Palabok"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0"></Button>
</Grid>
</UserControl>
then I implement it to my WPF Window:
<Window x:Class="NeocClinic.WPFSystem.IrritantDetailsForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windowsControls="clr-namespace:NeocClinic.WPFSystem.Templatas"
Title="IrritantDetailsForm" Height="300" Width="300">
<windowsControls:Palabok ></windowsControls:Palabok>
</Window>
My problem is I want to add a Group Box (with personalized components/controls of Irritants) to the Window that will be displayed in the Grid.Column="2" of the Palabok user control. Then I will use the same user control to another WPF window but put an Image control to the Grid.Column="2" instead of a Group Box, of the Palabok user control, but I can's seem to put the controls inside the Palabok user control.
I think your Control should inherit from ItemsControl
Please check : http://drwpf.com/blog/category/item-containers/

Images being stretched in WPF RibbonRadioButton

I've already posted a message about this, but I've lost it! And I can paste code now too...
I have a RibbonRadioButton whose image is being stretched, XAML below (I haven't changed the code-behind from its default). I have checked that the PNG is at 96dpi but the problem remains.
XAML:
<Window x:Class="DashboardTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ribbon:Ribbon x:Name="Ribbon">
<ribbon:RibbonGroup x:Name="LiveStatusGroup"
Header="RibbonGroup Header">
<ribbon:RibbonRadioButton x:Name="Dashboard"
LargeImageSource="images\LiveStatus_Dashboard.png"
HorizontalContentAlignment="Center"
HorizontalAlignment="Center"
VerticalContentAlignment="Center"
VerticalAlignment="Center"
Label="RibbonRadioButton Label"
ToolTip="RibbonRadioButton ToolTip"/>
</ribbon:RibbonGroup>
</ribbon:Ribbon>
</Grid>
</Window>
Can anyone shed light on why this is happening? I am running Windows 7 with text size set to medium (125%).
J

Categories

Resources