简体   繁体   中英

how to create C# code for XAML Grid code?

hi how i can create c# code for this xaml code?

 <Grid Width="300" Height="330">
        <Grid.Effect>
            <DropShadowEffect
                BlurRadius="15"
                Direction="-90"
                Opacity=".2"
                RenderingBias="Quality"
                ShadowDepth="1" />
        </Grid.Effect>
        <Grid.OpacityMask>
            <VisualBrush Visual="{Binding ElementName=Border2}" />
        </Grid.OpacityMask>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height=".50*" />
            <RowDefinition Height=".5*" />
        </Grid.RowDefinitions>
        <Border
            x:Name="Border2"
            Grid.Row="0"
            Grid.RowSpan="4"
            Background="White"
            CornerRadius="5" />
        <Border
            Grid.Row="0"
            Grid.RowSpan="3"
            Background="{Binding BorderColor}" />
       <StackPanel
            Grid.Row="3"
            Margin="20,0,20,0"
            VerticalAlignment="Center">

            <StackPanel Orientation="Horizontal">

            </StackPanel>
        </StackPanel>
    </Grid>

i need to create many controls so i must craete ui from c# code. please tell me how i can do this? my problem is i cant create Grid.Effect in c#

You can create a Grid dynamically in C# code in this way:

Grid grid = new Grid()
{
    Width = 300,
    Height = 300
};
grid.Effect = new DropShadowEffect()
{
    BlurRadius = 15,
    Direction = -90,
    Opacity = .2,
    RenderingBias = RenderingBias.Quality,
    ShadowDepth = 1
};
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions.Add(new RowDefinition());
// ... add more rows
// Create children:
var border = new Border()
{
    Background = new SolidColorBrush(Colors.White),
    CornerRadius = new CornerRadius(5)
};
Grid.SetRow(border, 0);
Grid.SetRowSpan(border, 4);
grid.Children.Add(border);
// ... add more children

I started to convert part of your XAML code here to show the principle. For more examples, you could also refer to this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM