1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
C#-Code :
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace FunctionLibraryTest1
{
public partial class Window1 : Window
{
public Window1() { InitializeComponent(); }
private void btnSplit_Click(object sender, RoutedEventArgs e)
{
lbDemo.ItemsSource = tbDemo.Text.SplitIntoWords();
lblDemo.Content = tbDemo.Text.CountWords();
}
}
public static class ExtendedStringMethods
{
public static MatchCollection SplitIntoWords(this string strToSplit)
{
return Regex.Matches( strToSplit, @"\w{1,}" );
}
public static string[] GetWordArray(this string strToSplit)
{
List<string> strWords = new List<string>();
foreach (Match word in Regex.Matches( strToSplit, @"\w{1,}" ))
strWords.Add( word.ToString() );
return strWords.ToArray();
}
public static int CountWords(this string strToCount)
{
return Regex.Matches( strToCount, @"\w{1,}" ).Count;
}
}
}
XAML-Code :
<Window x:Class="FunctionLibraryTest1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title=".Net-Snippets ExtendedStringMethods" Height="400" Width="300">
<Window.Resources>
<!-- **************************************************************************************** <-->
<!-- ***************************** Hintergrund der ListViews ******************************** <-->
<!-- **************************************************************************************** <-->
<LinearGradientBrush x:Key="ListViewBackgroundDark" StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="Black"/>
<GradientStop Offset="0.8" Color="DimGray"/>
<GradientStop Offset="1" Color="DimGray"/>
</LinearGradientBrush>
<!-- **************************************************************************************** <-->
<!-- **************************** Standard-Style für Buttons ******************************** <-->
<!-- **************************************************************************************** <-->
<Style x:Key="StandardButtonStyle" TargetType="Button">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>
<!-- **************************************************************************************** <-->
<!-- **************************** Template für schwarze Buttons ***************************** <-->
<!-- **************************************************************************************** <-->
<ControlTemplate x:Key="BlackButton" TargetType="{x:Type Button}">
<Border BorderBrush="White" BorderThickness="1" CornerRadius="3">
<Border BorderBrush="DarkGray" BorderThickness="1" CornerRadius="3">
<Grid Name="ButtonGrid">
<Rectangle Name="ButtonRect">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="Gray" />
<GradientStop Offset="0.49" Color="DimGray" />
<GradientStop Offset="0.5" Color="DimGray" />
<GradientStop Offset="1" Color="Black" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<!-- Bei Mausbewegung über den Button ... -->
<Trigger Property="IsMouseOver" Value="True">
<!-- Buttonhintergrund heller darstellen -->
<Setter Property="Fill" TargetName="ButtonRect">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="Silver" />
<GradientStop Offset="1" Color="Black" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<!-- Leuchteffekt -->
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="White" GlowSize="5" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="ButtonRect" Value="#FF000000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.Background>
<RadialGradientBrush GradientOrigin="0,0" Center="0.5,0.5" RadiusX="1.2" RadiusY="1.2">
<RadialGradientBrush.GradientStops>
<GradientStop Color="LightBlue" Offset="0" />
<GradientStop Color="Black" Offset="0.8" />
<GradientStop Color="Black" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="100" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox x:Name="lbDemo" Grid.Row="0" Width="170" Height="172" Margin="0,15,0,0" FontSize="14"
HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="White"
Background="{StaticResource ListViewBackgroundDark}"/>
<Label x:Name="lblDemo" Grid.Row="0" Width="50" Margin="0,10,0,0" FontSize="14"
Content="0" Foreground="White" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<TextBox x:Name="tbDemo" Grid.Row="1" Width="200" Height="75" Margin="0,10,0,0" FontSize="14"
HorizontalAlignment="Center" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto"
TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" Foreground="White"
Background="Black"/>
<Button x:Name="btnSplit" Grid.Row="2" Width="180" Margin="0,0,0,20"
Content="Text in einzelne Wörter aufteilen" Click="btnSplit_Click"
Style="{StaticResource StandardButtonStyle}" Template="{DynamicResource BlackButton}"
HorizontalAlignment="Center" VerticalAlignment="Bottom" />
</Grid>
</Window>
|