WPF, Color, brush |
How to set a color with code behind? |
Wie setzt man eine Hintergrundfarbe mittels C#? |
byte red=255; byte green=255; byte blue=0; byte tranz=0; tTestBox.Foreground = new SolidColorBrush( Color.FromArgb(tranz, red, green, blue) ); tTestBox.Foreground = new SolidColorBrush( Color.Fromrgb(red, green, blue) ); |
WPF, OpenDialog, InitialDirectory |
Openfiledialog with filter |
OpenDialog mit Startverzeichnis setzen |
using Microsoft.Win32;// opendialog OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = System.Environment.CurrentDirectory;
Complete Examples: opendialog.txt |
WPF, DrawText, Vertical |
Using Drawtext in OnRender with horizontal and vertical |
Wie benutzt man DrawText in OnRender mit vertikaler und horizontaler Ausrichtung |
int x = 100; int y = 100; formattedText = new FormattedText(text.Text, CultureInfo.GetCultureInfo("DE-de"), FlowDirection.LeftToRight, new Typeface("Verdana"), 24, Brushes.Red);
// important angle with x and y int angle=-90; RotateTransform RT = new RotateTransform(angle, x, y); dc.PushTransform(RT); dc.DrawText(formattedText, new Point(x, y)); dc.Pop();
Complete Examples: drawText.txt |
WPF, Colordialog |
In WPF, there is no colordialog. Use this colordialog. It is modal and you can select a color with alpha-value |
Den Colordialog gibt es in WPF nicht. Also Nachprogrammieren und einsetzen. Oder man benutzt den WinForms-Color-Dialog. |
Execute: ColorDialog dialog = new ColorDialog(); dialog.setColor(colorLine); dialog.ShowDialog(); if (dialog.Retcode) { colorLine = Color.FromArgb(dialog.Alpha, dialog.Red, dialog.Green, dialog.Blue); bnLineColor.Background = new SolidColorBrush(colorLine); }
Complete Examples: colordialog.txt
Image: colordialog.png |
WPF, Grid row hidden row collapse |
set the height of a row in a grid to zero |
Um eine Zeile zu löschen, setzt man die Zeile auf die Höhe null. |
XAML: <Grid x:Name="grid" >
CS: public void setRow3Visible(bool visible) { __if (visible) { __grid.RowDefinitions[3].Height = new GridLength(0); _} _else { __grid.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Star); _} } |
WPF, Spliterdialog à la Explorer vertical Spliter |
A Template with a Grid and a GridSplitter to simulate an Explorerdialog without any Buttons and any menues |
Ein Template mit einem Grid und einem GridSplitter, um ein Explorerdialog zu simulieren pures Grid |
Image: splitter1.png
Complete Examples: SplitContainer1.7z Sample: <GridSplitter Name="splitterline" Grid.Column="0" Grid.Row="0" Width="3" Background="Red" /> Tip: No extra column |
WPF, Spliterdialog à la Explorer vertical Spliter |
A Template with a Grid and a GridSplitter to simulate an Explorerdialog with some Buttons and somemenues. It shows the Communication from Commands between Menues and Buttons |
Ein Template mit einem Grid und einem GridSplitter, um ein Explorerdialog zu simulieren Beinhaltet auch Menüs und Schalter. Es zeigt, wie die Kommunikation zwischen Menüs und Schaltern funktioniert (Commands) |
Image: splitter2.png
Complete Examples: SplitContainer2.7z Sample: <GridSplitter Name="splitterline" Grid.Column="0" Grid.Row="0" Width="3" Background="Red" /> Tip: No extra column |
WPF, Horizontaler Splitter |
An horozontal splitter is easy to create with a grid. But one must give the splitter an separate cell/row. |
Ein horizontaler Splitter ist recht einfach mit einem Grid zu erstellen. Aber man muss den Splitter eine eigene Reihe geben. |
Sample: <GridSplitter Name="gridsplitter" Grid.Column="0" Grid.Row="1" Background="Red" Height="4" HorizontalAlignment="Stretch" /> Tip: One need a new column for the splitter |
WPF, vertical Splitter Position |
How can I get and set the width of a splitter? |
Wie kann man die Breite eines Splitter holen und setzen? |
get: ColumnDefinitionCollection cols = grid.ColumnDefinitions; double w = cols[0].ActualWidth; WriteRegistryInt(C_REG_ADR, "Splitterwidth", w);
set: int w = ReadRegistryInt(C_REG_ADR, "splitterWidth"); if (w>50 && w<1000) { ColumnDefinitionCollection cols = grid.ColumnDefinitions; cols[0].Width = new GridLength(w); } |
WPF, horizontaler Splitter Position |
How can I get and set the height of a splitter? |
Wie kann man die Höhe eines Splitter holen und setzen? |
get: RowDefinitionCollection rows = splitContainer1.RowDefinitions; int h = (int)rows[1].ActualHeight; WriteRegistryInt(C_REG_ADR, "Splitterheight", h);
set: int h = ReadRegistryInt(C_REG_ADR, "Splitterheight"); if (h>20 && h<700) { RowDefinitionCollection rows = splitContainer1.RowDefinitions; rows[1].Height = new GridLength(h); } |
WPF, Label, Border |
How can I get a label with a border? |
Wie kann man ein Label mit einem Rand (Strich umranden)? |
<Border CornerRadius="1" BorderBrush="Gray" BorderThickness="2" Margin="5"> __<Label Content="Daten verwalten" Foreground="Blue" /> </Border> |
WPF, Buttons, Image |
How can I get a button with an image? |
Wie erhält man einen Schalter mit ein Bild? |
<Button Grid.Column="7" Grid.Row="0" Name="bnFirst" Margin="20 0 0 0" ToolTip="Ersten Datensatz anzeigen" IsTabStop="False" Height="25" Width="25" Click="bnFirst_Click"> _<Image Source="/IGMStart;component/Images/first.png" Height="23" Width="23" /> </Button> |
WPF, Buttons, Label, Image |
How can I get a button with a label and an image? |
Wie erhält man einen Schalter mit einem Label und einem Bild? |
<Button Grid.Column="7" Grid.Row="0" Name="bnStart" Margin="20 0 0 0" IsTabStop="False" Height="25" Width="25" Click="bnStart_Click"> _<StackPanel> ___<Image Source="/IGMStart;component/Images/start.png" Height="23" Width="23" /> ___<Label Content="Starten der Prüfung" /> _</StackPanel> </Button> |
WPF, ListView edit items |
How edit a listview/gridview row ? |
Wie kann man in einer ListView eine Zeile, ein Objekt editeren? |
MyClass cl; int i = listView1.SelectedIndex; if (i >= 0) { cl = (MyClass)listView1.Items[i]; // Dialog cl.attribut="new Value"; listView1.Items[i] = cl; listView1.Items.Refresh(); } |
WPF, ListView DoubleClick |
How can one implement a double-Click-Event in a listView ? |
Wie kann man einen Double-Click-Event in eier ListView realisieren? |
private void Window_Loaded(object sender, RoutedEventArgs e) { listView1.AddHandler(Control.MouseDoubleClickEvent, new RoutedEventHandler(HandleDoubleClick)); } protected void HandleDoubleClick(object sender, RoutedEventArgs e) { mnEdit_Click(sender, e); }
private void mnEdit_Click(object sender, RoutedEventArgs e) { // see also previous row } |
WPF tree selecteditem select node show selected node |
How can I select a node and show the node in the tree? |
Wie kann ich einen Knoten markieren und anzeigen? |
// selected treenode: tree.SelectedValuePath="root\abc\cde\edf"; TreeViewItem node = tree.selectedValue; node.IsSelected = true; node.BringIntoView(); tree.Focus(); |
WPF, DockPanel with C# |
How can I create a DockPanel and insert GUI-Elements? |
Wie kann man einen DockPanel erzeugen und Elemente einfügen? |
Label label1 = new Label(); Label label2 = new Label(); TextBox editor = new TextBox();
WrapPanel wrapPanel = new WrapPanel(); wrapPanel.HorizontalAlignment = HorizontalAlignment.Center; wrapPanel.Children.Add( label1 ); wrapPanel.Children.Add( label2 );
DockPanel dockpanel = new DockPanel(); dockpanel.LastChildFill = true; // Important DockPanel.SetDock(wrapPanel, Dock.Bottom); DockPanel.SetDock(editor, Dock.Top); dockpanel.Children.Add(flowLayoutPanel1); dockpanel.Children.Add(editor); |
WPF, TextBox row column |
how can one get the actual row and column? |
Wie kann man die aktuelle Zeile und Spalte erhalten? |
TextBox editor; int charIndex = editor.CaretIndex; int lineindex = (editor.GetLineIndexFromCharacterIndex(charIndex) + 1); int index_aktLine = editor.GetCharacterIndexFromLineIndex(lineindex - 1); lRowNr.Content = "Zl: " + lineindex; int columnCount = editor.SelectionStart - index_aktLine + 1; lColumnNr.Content = "Sp: " + columnCount; |
WPF, Label underline is not visible |
in a String ist an underline "_". The Label delete this. What can I do? |
Ein Label-Element löscht ein Underline "_". Abhilfe? |
Label lDatei; String sValue="bsp_001.txt"; lDatei.Content = sValue.Replace("_","__"); |
WPF Label Textbox Checkbox Radiobutton Margin Code behind |
How can I set a margin with code behind? |
Wie kann ich einen Rand in C# setzen? |
CheckBox chk = new CheckBox(); Thickness margin = chk.Margin; margin.Left = margin.Top = margin.Bottom = margin.Right = 5; chk.Margin = margin; |
WPF, TextBlock |
How can I create a TextBlock in XAML? |
Wie gebe ich einen TextBlock in XAML ein? |
<TextBlock> __Hello! I am a TextBlock. __<LineBreak /> __Hello! I am a TextBlock. __<LineBreak /> </TextBlock> |
WPF, TextBlock |
How can I create a TextBlock in XAML with "Tabs" ? |
Wie gebe ich einen TextBlock mit Einrückungen in XAML ein? |
<TextBlock> __<Run FontWeight="Bold" FontSize="14" Text="Ablauf:" /> __<LineBreak /> __<Run FontStyle="Italic" FontSize="12" Text=" • Installieren der Dos-Box" /> __<LineBreak /> __<Run TextDecorations="Underline" FontSize="12" Text=" • Anlegen eines Verzeichnisses (jeweils maximal 8 Buchstaben)" /> __<LineBreak /> __<Run FontSize="12" Text=" o Beispielsweise: D:\asm" /> __<LineBreak /> </TextBlock> |
WPF, TextBlock, Line break |
How can I create a line break? |
Wie erzeuge ich einen Zeilenumbruch? |
<LineBreak /> |
WPF, TextBlock, Code behind |
How can I create a TextBlock mit C#? |
Wie erzeuge ich mit C# einen TextBlock? |
TextBlock txtBlock = new TextBlock(); txtBlock.Height = 30; txtBlock.Width = 250; txtBlock.Text = "I'm a TextBlock"; txtBlock.Foreground = new SolidColorBrush(Colors.Blue); myContainer.Children.Add(txtBlock); |
WPF, TextBox, remove Undo |
How can I delete/remove the undo-Stack in a TextBox? |
Wi ekann ich den Undo-Stack löschen bzw. entfernen? |
You implement a loop: while (editor.Undo()) { } |
WPF, ListCheckBox |
In Winforms exists a ListCheckBox. Now in WPF also |
In Winforms gibt es eine ListCheckBox. Nun auch in WPF. |
ListCheckBox: Image: ListCheckBox1.jpg Quellcode: ListCheckBox1.txt
Projekt-Quellcode: ListCheckBox1.zip |
WPF, ListCheckBox |
In Winforms exists a ListCheckBox. Now in WPF also This example put the selected items in a second Listbox . This is also possible for RadionButtons !! |
In Winforms gibt es eine ListCheckBox. Nun auch in WPF. Dieses Beispiel kopiert die markierten Elemente in eine rechte zusätzliche Listbox. Damit erhält man eine bessere Übersicht. |
ListCheckBox: Bild: ListCheckBox2.jpg Quellcode: ListCheckBox2.txt
Projekt-Quellcode: ListCheckBox2.txt |
WPF, Timer |
how can one used a Timer? |
Wie kann man einen Timer realisieren? |
using System.Windows.Threading; // Timer
private void Window_Loaded(object sender, RoutedEventArgs e) { DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 1); // hour, min, sec // minmal one second, no millisecond /// with "FromMilliseconds" one can set millisecond dispatcherTimer.Interval = TimeSpan.FromMilliseconds(250); dispatcherTimer.Start(); } // window_Loaded
private void dispatcherTimer_Tick(object sender, EventArgs e) { // action CommandManager.InvalidateRequerySuggested(); } |
WPF DataGrid Columnwidth C# |
How can I change the Column in a grid with C#? |
Wie kann man eine Spalte in einem Grid mit C# ändern? |
<Grid Name="grid1" DockPanel.Dock="Top" > </Grid> ColumnDefinitionCollection cols = grid1.ColumnDefinitions; GridLength gl1; gl1 = new GridLength(200); cols[0].Width = gl1; |
WPF DataGrid Columnwidth C# |
How can I change the Column in a grid with C#? |
Wie kann man eine Spalte in einem Grid mit C# ändern? |
datagrid.Columns[0].Width = 80; datagrid.Columns[1].Width = 100; |
WPF DataGrid select a row |
how can I scoll the datagrid rows? |
wie kann ich eine Zeile in einem Datagrid ansteuern? |
1) Change the selectedIndex 2) scroll to View 3) Focus setzen
Prev-Event: datagrid.SelectedIndex = datagrid.SelectedIndex - 1; datagrid.ScrollIntoView(datagrid.SelectedItem); datagrid.Focus(); navigatorbar_datagrid.txt |
WPF Datagrid background cell |
how can I set the background of a cell ? |
wie kann ich den Background einer Spalte/Zelle setzen ? |
Image: DatagridBackground.png
DatagridBackground_MainWindow_xaml.txt DatagridBackground_MainWindow_cs.txt DatagridBackground_Model_Color.txt DatagridBackground_MyBkColorConverter1.txt DatagridBackground_MyBkColorConverter2.txt
Project: DatagridBackground.zip |
WPF CardControl |
How can I create a CardLayout à la Java? |
Wie kann man einen Cardlayout à la Java erstellen? Die Elemente liegen alle übereinander. |
Solution: 1) create a TabControl 2) Insert the GUI-Elements 3) Set the Height to Zero 4) Delete Focus for the TabItem
foreach (TabItem tabitem in tabControl1.Items) { tabitem.Height = 0; tabitem.Focusable = false; }
Beispielprojekt: CardLayout.zip |
WPF TabControl mit CloseButton |
How can I create a TabControl mit a CloseButton in every TabItem? |
Wie erhalte ich einen Schalter in jedem TabItem eines TabControls? |
Ablauf: Ableiten eines TabItems: MyTabItem Erzeugen eines UserControls Interface für das Schließen Interface in main implementieren Beim Erzeugen MyTabItem verwenden
Beispielprojekt: TabControl_CloseButton.zip
Bild: TabControl_CloseButton.jpg |
WPF TabControl ListView |
How can I create a ListView complete with C#? How can I add Events: SelectionChange DoubleClick HeaderClick (sort) |
Wie kann man eine ListView komplett mit C# programmieren? Wie kann man Events hinzufügen: SelectionChange DoubleClick HeaderClick (sort) |
ListView mit TabControl ListView komplett mit C# TabControl mit Schalter zum Beenden Quellcode: TabControl_ListView.zip |
WPF GroupBox |
How can I create a GroupBox? Also with an Image? |
Wie kann man eine GroupBox erstellen? Im Header soll auch ein Bild sein! |
GroupBox1 Einfache GroupBox mit ComboBox Projekt-Quellcode: GroupBox1.zip GroupBox: GroupBox1.txt
GroupBox2 GroupBox mit ComboBox Die Überschift ist eine Zusammenfassung Image mit Label Projekt-Quellcode: GroupBox2.zip GroupBox: GroupBox2.txt |
WPF FileWatcher |
How can I create a FileWatcher with Code behind? |
Wie kann man manuell einen Filewatcher erzeugen? |
FileWatcher Erstellen eines Filewatchers Quellcode: filewatcher.txt |
WPF: set Cursor |
How can I set a Cursor Wait? |
Wie kann man einen Pausenkursor setzen? |
Mouse.OverrideCursor = Cursors.Wait; try { // Action } finally { Mouse.OverrideCursor = null; } |
WPF webbrowser local file |
How can I set a file to an webbrowser? |
Wie kann man einem Webbrowser eine lokale Datei zuweisen? |
webbrowser.Navigate("file:///" + filename ); |
WPF Usercontrol |
How can I insert a usercontrol? |
Wie kann ich ein Usercontrol einfügen? |
XAML: 1) Link to the Project "MyProject" xmlns:local="clr-namespace:MyProject"
2) Insert the UI-Element <local:MyUserControl x:Name="myuserControl" /> |
WPF Focus TextBox SelectAll |
What can I do, that with a tab the selected textbox ist fill selected? |
Was muss man Einfügen, um bei einem Tabwechsel die Ziel-TextBox selektiert zu haben? |
textbox_selectall |
WPF selectDirectory FolderBrowserDialog |
How can I select a directory? |
Wie kann man ein Verzeichnis auswählen ? |
// externer Verweis eintragen // ProjektTree, Assemblies, Framework System.Windows.Forms private void bnSelectDirectory_Click(object sender, RoutedEventArgs e) { _System.Windows.Forms.FolderBrowserDialog dialog = new _System.Windows.Forms.FolderBrowserDialog(); _dialog.SelectedPath = System.Environment.CurrentDirectory; _System.Windows.Forms.DialogResult result = dialog.ShowDialog(); _if (result == System.Windows.Forms.DialogResult.OK) { ___aktion(dialog.SelectedPath); _} } |
WPF selectDirectory FolderBrowserDialog |
How can I use a FolderBrowserDialog in WPF? |
Wie kann man ein FolderBrowserDialog, Auswahl eines Verzeichnisses, in WPF benutzen? |
the BEST way is to build an own Dialog: Properties: - save and restore the path in the registry - select an initial folder - show the selected node with node.BringIntoView() - userdirs: docs, music, pictures, videos, desktop
Important: The four pictures must be in the programmfolders of the program Pictures: FolderBrowser1.png FolderBrowser2.png
Project: FolderBrowser.zip
|
WPF selectDirectory FolderBrowserDialog |
How can I use a FolderBrowserDialog in WPF AND show the files in the actual folder? |
Wie kann man ein FolderBrowserDialog, Auswahl eines Verzeichnisses, in WPF benutzen und wie kann man Dateien rehts neben dem Baum anzeigen? |
the BEST way is to build an own Dialog: Properties: - save and restore the path in the registry - select an initial folder - show the selected node with node.BringIntoView() - userdirs: docs, music, pictures, videos, desktop - show the files in a listview
Important: The four pictures must be in the programmfolders of the program Pictures: FolderBrowser1.png FolderBrowser3.png
Project: FolderBrowser2.zip
|
WPF Popupmenu |
How can I create a ContextMenu or a popupmenu? |
Wie kann man ein ContextMenu oder ein Popupmenu erzeugen? |
Contextmenu |
UAC User Access Control |
I'm a user-admin, but my program has no rights. How can I insert a UAC? |
|
As User-Admin one has not Admin-Control. You must insert a User Access Control in your application.
Insert a UAC: Menu "Project -> Add new item -> Application Manifest file" (app.manifest)
UAC1.png
there are three values: - asInvoker - requireAdministrator - highestAvailable.
Change line 19 from "asInvoker" to "requireAdministrator".
app.mainfest.txt |
WPF PrimaryScreen set top, left |
How can I get and set the postion of a window? Especially with two screens. |
Wie kann man die Bildschirmposition des aktuellen Fensters speichern und restaurieren? |
In the attribs top, legt, width, height are the actual values. With two or more screens windows set the coordinates. But it can be negative (<0). C# save the resolution of the screen in four values: - VirtualScreenLeft - VirtualScreenTop - VirtualScreenWidth - VirtualScreenHeight
Examples: One screen with one width 1900 pixel: One screen with one width 1000 pixel: In one position you get: - VirtualScreenLeft: 0 - VirtualScreenWidth: 2900
In other position you get: - VirtualScreenLeft: -1900 - VirtualScreenWidth: 1000
In third position you can get: - VirtualScreenLeft: -1000 - VirtualScreenWidth: 1900
So you save the position in the registry and compare the values with the four VirtualScreen-Values source |