2020년 12월 5일 토요일

[IronPython] WPF Grid Layout

WPF Grid Layout

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore");
clr.AddReference('System.Data')
clr.AddReference('System.Windows.Forms')

import System
from System import *
from System.Data import *
from System.Type import GetType
from System.Threading.Tasks import Task
from System.Windows import *
from System.Windows.Controls import *
from System.Windows.Controls.Primitives import *
from System.Windows.Media import *
from System.Windows.Media.Imaging import *
from System.Windows.Media.Effects import *
from System.Windows.Shapes import Rectangle
from System.Windows.Data import Binding
from System.Windows.Ink import Stroke
from System.Windows.Forms import ( FolderBrowserDialog, DialogResult )
from Microsoft.Win32 import ( OpenFileDialog, SaveFileDialog )

class WpfGrid():
    def __init__(self,row=None,col=None):
        self.ctrl = Grid()
    def add_row(self,height=1,expand=False,span=1):
        if expand: length = GridLength(height, GridUnitType.Star)
        else:      length = GridLength(height, GridUnitType.Auto)
        self.ctrl.RowDefinitions.Add(RowDefinition(Height = length))
    def add_column(self,width=1,expand=False,span=1):
        if expand: length = GridLength(width, GridUnitType.Star)
        else:      length = GridLength(width, GridUnitType.Auto)
        self.ctrl.ColumnDefinitions.Add(ColumnDefinition(Width = length))
    def add_item(self,item,row,col,rowspan=1,colspan=1):
        Grid.SetRow(item, row);
        Grid.SetColumn(item, col);
        self.ctrl.Children.Add(item)
        if rowspan > 1: item.SetValue(Grid.RowSpanProperty, rowspan);
        if colspan > 1: item.SetValue(Grid.ColumnSpanProperty, colspan);
     
class WpfBorderGrid():
    def __init__(self):
        self.grid = Grid()
        self.ctrl = Border()
        self.ctrl.HorizontalAlignment = HorizontalAlignment.Left
        self.ctrl.VerticalAlignment = VerticalAlignment.Top
        #border.BorderBrush = BorderBrush.Black
        self.ctrl.BorderThickness = Thickness(2)
        #self.ctrl.Content = self.grid
    def add_row(self,height=1,expand=False,span=1):
        if expand: length = GridLength(height, GridUnitType.Star)
        else:      length = GridLength(height, GridUnitType.Auto)
        self.grid.RowDefinitions.Add(RowDefinition(Height = length))
    def add_column(self,width=1,expand=False,span=1):
        if expand: length = GridLength(width, GridUnitType.Star)
        else:      length = GridLength(width, GridUnitType.Auto)
        self.grid.ColumnDefinitions.Add(ColumnDefinition(Width = length))
    def add_item(self,item,row,col,rowspan=1,colspan=1):
        Grid.SetRow(item, row);
        Grid.SetColumn(item, col);
        self.grid.Children.Add(item)
        if rowspan > 1: item.SetValue(Grid.RowSpanProperty, rowspan);
        if colspan > 1: item.SetValue(Grid.ColumnSpanProperty, colspan);
       
            
if __name__ == "__main__":
    import win
    app = win.Win("HSplit Demo",320,240)
    b00 = Button(); b00.Content = "00"
    b01 = Button(); b01.Content = "01"
    b10 = Button(); b10.Content = "10"
    b20 = Button(); b20.Content = "20"
    g = WpfGrid()
    g.add_row(); g.add_row(expand=True); g.add_row()
    g.add_column(); g.add_column(expand=True)
    g.add_item(b00,0,0)
    g.add_item(b01,0,1)
    g.add_item(b10,1,0,colspan=2)
    g.add_item(b20,2,0,colspan=2)
    app.set_content(g.ctrl)
    app.run()
    
    

댓글 없음:

댓글 쓰기