Window1.xaml

advertisement
WPF-Example with DataGrid
This Solution is a test example for WPF Data Binding and has some code to handle a DataGrid and also some code for testing ConverterCulture,
especially for de-CH (German for Switzerland). It gives different results running with Windows 8.1 or Windows 7.
Window1.xaml
<Window x:Class="WPFApplDataGrid.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"
Title="{Binding Titel}"
Height="450" Width="400" WindowStartupLocation="CenterScreen">
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Label>DataGrid (erweiterbar)</Label>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Name="PBBerechnen" Width="80" Margin="5,5" IsEnabled="False" Click="PBBerechnen_Click">Berechnen</Button>
<Button Name="PBSchliessen" Width="80" Margin="5,5" Click="PBSchliessen_Click">Schliessen</Button>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Margin="0,0,0,10">
<!-- Variantes of ConverterCulture, 2nd line should give same result as 1st line -->
<TextBlock Text="{Binding TotBetrag, StringFormat=N02, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}"
Margin="10,0,0,0" VerticalAlignment="Center" MinWidth="5"></TextBlock>
<TextBlock Text="{Binding TotBetrag, StringFormat=N02, ConverterCulture=de-CH}" Margin="10,0,0,0"
VerticalAlignment="Center" MinWidth="5"></TextBlock>
<TextBlock Text="{Binding TotBetrag, StringFormat=N02, ConverterCulture=de-DE}" Margin="10,0,0,0"
VerticalAlignment="Center" MinWidth="5"></TextBlock>
<TextBlock Text="{Binding TotBetrag, StringFormat=N02, ConverterCulture=en-US}" Margin="10,0,0,0"
VerticalAlignment="Center" MinWidth="5"></TextBlock>
<!-- next line takes default (en-US), result is same as line before -->
<TextBlock Text="{Binding TotBetrag, StringFormat=N02}" Margin="10,0,0,0" VerticalAlignment="Center"
MinWidth="5"></TextBlock>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Margin="0, 10">
<Label VerticalAlignment="Center">%-Satz</Label>
<TextBox Text="{Binding Prozent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="50"
HorizontalContentAlignment="Right" VerticalAlignment="Center"/>
<Label Margin="10,0,0,0" VerticalAlignment="Center">Anazhl Zeilen</Label>
<TextBlock Text="{Binding Anzahl}" Margin="5,0" VerticalAlignment="Center" MinWidth="5"></TextBlock>
<TextBlock Text="{Binding CultureCode}" Margin="10,0,0,0" VerticalAlignment="Center" MinWidth="5"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<DataGrid Name="DGWerte"
Margin="10,0,0,0"
AutoGenerateColumns="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserReorderColumns="False"
CanUserSortColumns="True"
CanUserResizeColumns="True"
CanUserResizeRows="False"
IsSynchronizedWithCurrentItem="True"
SelectionMode="Single"
SelectionUnit="FullRow"
GridLinesVisibility="All"
ToolTip="Selektieren für Detail"
Background="White">
<DataGrid.Columns>
<DataGridTextColumn MinWidth="100"
Binding="{Binding Path=EingBetrag, StringFormat=N02, ConverterCulture={x:Static
gl:CultureInfo.CurrentCulture}, Mode=TwoWay}"
Header= "Betrag">
<DataGridTextColumn.HeaderStyle>
<Style>
<Setter Property="Label.HorizontalContentAlignment" Value="Right" />
</Style>
</DataGridTextColumn.HeaderStyle>
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn MinWidth="100"
Binding="{Binding Path=EingBetragString, Mode=OneWay}"
Header= "String-Betrag">
<!-- EingBetr::ToString("N02") -->
<DataGridTextColumn.HeaderStyle>
<Style>
<Setter Property="Label.HorizontalContentAlignment" Value="Right" />
</Style>
</DataGridTextColumn.HeaderStyle>
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn MinWidth="100"
Binding="{Binding Path=ProzBetrag, StringFormat=N04, ConverterCulture=de-CH,
Mode=OneWay}"
Header= "%-Betrag">
<!-- ConverterCulture shown is not de-CH -->
<DataGridTextColumn.HeaderStyle>
<Style>
<Setter Property="Label.HorizontalContentAlignment" Value="Right" />
</Style>
</DataGridTextColumn.HeaderStyle>
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</DockPanel>
</Window>
Window1.xaml.cbl
class-id WPFApplDataGrid.Window1 is partial
inherits type System.Windows.Window.
working-storage section.
01 model type WPFApplDataGrid.Window1Model.
method-id NEW.
procedure division.
*
set type System.Threading.Thread::CurrentThread::CurrentUICulture to type
System.Threading.Thread::CurrentThread::CurrentCulture.
*
set type System.Threading.Thread::CurrentThread::CurrentUICulture to type
System.Globalization.CultureInfo::CurrentCulture.
invoke self::InitializeComponent()
set model to new type WPFApplDataGrid.Window1Model(self)
set model::Titel to "WPF-Fenster mit DataGrid"
set self::DataContext to model.
goback.
end method.
method-id PBBerechnen_Click final private.
procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
invoke model::GridBerechnen()
exit method.
end method.
method-id PBSchliessen_Click final private.
procedure division using by value sender as object e as type System.Windows.RoutedEventArgs.
invoke self::Close().
end method.
end class.
Window1Model.cbl
$set ilusing"System.ComponentModel"
class-id WPFApplDataGrid.Window1Model implements type INotifyPropertyChanged.
working-storage section.
01
GridSource type WPFApplDataGrid.GridListe.
01
Window1
type WPFApplDataGrid.Window1.
01
01
01
Proz
Anz
TotBetr
decimal.
binary-long.
decimal.
01
PropertyChanged type PropertyChangedEventHandler event public.
01
Titel string property.
method-id. NEW.
procedure division using by value Win as type WPFApplDataGrid.Window1.
set Window1 to Win.
invoke self::InitDataGrid()
exit method.
end method.
*>> <summary>
*>> DataGrid initialisieren
*>> </summary>
method-id. InitDataGrid.
local-storage section.
01 i1 binary-long.
01 Betrag pic s9(10)v99 comp-3 value 12345.75.
01 GridZl type WPFApplDataGrid.GridZeile.
procedure division.
set GridSource to new WPFApplDataGrid.GridListe()
perform varying i1 from 1 by 1
until i1 > 5
set GridZl to new WPFApplDataGrid.GridZeile()
set GridZl::EingBetrag to Betrag
invoke GridSource::Add(GridZl)
compute Betrag = Betrag * i1
end-perform
set self::Prozent to 10
exit method.
end method.
method-id. GridBerechnen.
local-storage section.
*01 i1 binary-long.
*01 lg binary-long.
01 GZl type WPFApplDataGrid.GridZeile.
Err
procedure division.
*
!!!! 'ElementAt' ergibt einen Fehler.
--> Korrigiert mit Update 2.2.1 Mai 2014 VS 2013
*
set lg to GridSource::Count()
*
perform varying i1 from 0 by 1
*
until i1 >= lg
*
set GZl to GridSource::ElementAt(i1)
*> Fehler: Could not find method 'ElementAt' with this signature
*
set GZl to GridSource[i1]
*> OK
*
set GZl to GridSource::Item(i1)
*> OK
*
set GZl::ProzBetrag to GZl::EingBetrag * Prozent / 100
*
end-perform.
move 0 to TotBetr.
perform varying GZl thru GridSource
*> Dies ist die elegantere Version für Cobol
set GZl::ProzBetrag to GZl::EingBetrag * Proz / 100
set TotBetr to TotBetr + GZl::EingBetrag
end-perform
set self::Anzahl to GridSource::Count()
set self::TotBetrag to TotBetr
set Window1::DGWerte::ItemsSource to null
set Window1::DGWerte::ItemsSource to GridSource
exit method.
end method.
property-id Anzahl binary-long.
getter.
set property-value to Anz
setter.
set Anz to property-value
invoke self::OnPropertyChanged("Anzahl")
end property.
property-id TotBetrag decimal.
getter.
set property-value to TotBetr
setter.
set TotBetr to property-value
invoke self::OnPropertyChanged("TotBetrag")
end property.
property-id Prozent decimal.
getter.
set property-value to Proz
setter.
set Proz to property-value
invoke self::OnPropertyChanged("Prozent")
if Proz > 0
set Window1::PBBerechnen::IsEnabled to true
else
set Window1::PBBerechnen::IsEnabled to false
end-if.
invoke self::GridBerechnen().
end property.
property-id CultureCode string.
getter.
set property-value to type System.Globalization.CultureInfo::CurrentCulture::ToString() & ", UI: " &
type System.Globalization.CultureInfo::CurrentUICulture::ToString().
end property.
method-id. OnPropertyChanged protected.
01 args type PropertyChangedEventArgs.
procedure division using propname as string.
set args to new PropertyChangedEventArgs(propname)
if PropertyChanged not = null
invoke PropertyChanged::Invoke(self, args)
end-if.
end method.
end class.
*>> <summary>
*>> Collection with Grid Line Details
*>> </summary>
class-id WPFApplDataGrid.GridListe inherits type System.Collections.ObjectModel.ObservableCollection[type
WPFApplDataGrid.GridZeile].
end class.
*>> <summary>
*>> Detail for Grid Line
*>> </summary>
class-id WPFApplDataGrid.GridZeile.
01
EingBetr decimal.
01
ProzBetrag decimal property.
property-id EingBetrag decimal.
getter.
set property-value to EingBetr.
setter.
set EingBetr to property-value.
end property.
*> getter/setter generated by Cobol-Compiler
*> --> Konversion durch Binding, nicht korrekt für de-CH
property-id EingBetragString string.
getter.
set property-value to EingBetr::ToString("N02").
end property.
end class.
*> --> Korrekte Anzeige
Download