Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GroupBox control in UWP?

Tags:

xaml

uwp

Getting acquainted with UWP. I'm developing an App for simulating electric circuits. There is a classic visual control called Frame, later called GroupBox in WPF. It seems this control is absent in UWP. There is a control called HeaderedContentControl in UWP.Toolkit library, but doesn't look the same. And seems the background and border properties don't work..

currently my code is:

  <controls:HeaderedContentControl Margin="5" 
                                         BorderBrush="Black" BorderThickness="1"
                                         HorizontalAlignment="Stretch"
                                         HorizontalContentAlignment="Stretch">
            <controls:HeaderedContentControl.Header>
                <Border BorderBrush="Black" BorderThickness="1">
                    <Border.RenderTransform>
                        <TranslateTransform Y="-10"/>
                    </Border.RenderTransform>
                    <TextBlock Text="Resistor Value"/>
                </Border>
            </controls:HeaderedContentControl.Header>

            <local:ComponentValueBox Unit="Ohm" HorizontalAlignment="Left"
                                     Value="{x:Bind resistorValue, Mode=TwoWay}"
                                     ValueChanged="changeR"/>

        </controls:HeaderedContentControl>

And what I see (in the flyout) is: My Resistor Parameters Flyout

Not quite like the GroupBox control.. What I would like to see is something like following:

enter image description here

What Should I do?

like image 815
Leandro Alsina Avatar asked Jan 28 '23 17:01

Leandro Alsina


2 Answers

UWP is different from WPF. UWP is based on windows runtime, WPF is based on .NET Framework. They all use XAML to layout UI elments, but they have different XAML rendering engine. You could not think that MS dropped the old classic control. They're totally on the different platform. We call 'UWP' as Unversal Windows Platform. For now, you're not able to find such a 'GroupBox', but it's a new platform, you might be able to see such a control in the future. Anything is possible.

For your requirement, like @Muzib said, you entirely could make a custom control to meet your requirement. I used UserControl TextBlock Border ContentControl to make such a 'GroupBox' for your reference.

Please see my following code sample:

<UserControl
x:Class="AppGroupBox.GroupBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppGroupBox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
    <TextBlock x:Name="HeaderTitle" Text="Header" Margin="7 0 0 0" LayoutUpdated="HeaderTitle_LayoutUpdated"></TextBlock>
    <Border BorderBrush="Black" x:Name="border" BorderThickness="0 2 0 0" Margin="100 10 3 3" CornerRadius="0 5 0 0"></Border>
    <Border BorderBrush="Black" BorderThickness="2 0 2 2" Margin="3 10 3 3" CornerRadius="5">
        <ContentControl x:Name="Content" Margin="10 10 10 10">
        </ContentControl>
    </Border>
</Grid>

public sealed partial class GroupBox : UserControl
{
    public GroupBox()
    {
        this.InitializeComponent();
    }



    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(string), typeof(GroupBox), new PropertyMetadata("Your Header", HeaderPropertyChangedCallback));

    public static void HeaderPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue != e.OldValue)
        {
            (d as GroupBox).HeaderTitle.Text = e.NewValue?.ToString();
            //(d as GroupBox).border.Margin = new Thickness((d as GroupBox).HeaderTitle.ActualWidth, 10, 3, 3);
        }
    }

    public object CustomContent
    {
        get { return (object)GetValue(CustomContentProperty); }
        set { SetValue(CustomContentProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CustomContentProperty =
        DependencyProperty.Register("CustomContent", typeof(object), typeof(GroupBox), new PropertyMetadata(null,PropertyChangedCallback));

    public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue != e.OldValue)
        {
            (d as GroupBox).Content.Content = e.NewValue;
        }
    }

    private void HeaderTitle_LayoutUpdated(object sender, object e)
    {
        border.Margin = new Thickness(HeaderTitle.ActualWidth+10,10,3,3);
    }
}
<local:GroupBox Header="My GroupBox" Height="300" Width="500">
        <local:GroupBox.CustomContent>
            <StackPanel>
                <RadioButton Content="r1"></RadioButton>
                <TextBox></TextBox>
            </StackPanel>
        </local:GroupBox.CustomContent>
</local:GroupBox>

enter image description here

like image 189
Xie Steven Avatar answered Feb 26 '23 14:02

Xie Steven


I don't think there's such controls in UWP. Most probably you have to make your own CustomControl to achieve something that looks exactly lik that in UWP.

But hey, you can achieve something like that with a 'customized' ListView. Look at this:

<ListView Header="I am a header" BorderThickness="1" BorderBrush="Red" Width="250" Height="200" SelectionMode="None">
    <ListView.HeaderTemplate>
        <DataTemplate>
            <ListViewHeaderItem Content="{Binding}"/>
        </DataTemplate>
    </ListView.HeaderTemplate>
    <RadioButton>Any Value</RadioButton>
    <RadioButton>1% standard?</RadioButton>
    <RadioButton>5% standard</RadioButton>
</ListView>

It produces this output:

enter image description here

Of course You can make these items more dense if you want so.

like image 45
Muzib Avatar answered Feb 26 '23 14:02

Muzib