StrayThought
Topic Author
Posts: 6
Joined: 18 Nov 2021, 11:41

Unity/C# without XAML?

28 Feb 2024, 12:06

Are there any samples for building GUI entirely in C# without XAML?

My use-case is I have a bunch of controls in a scrollable list and need group headers, sliders, checkboxes and buttons in the scrolling list. I prefer to just build all of it in code rather than XAML. I can't seem to find any examples of how to build a complete GUI without using any XAML.
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Unity/C# without XAML?

29 Feb 2024, 12:22

Although it is possible just to create UI elements in code:
StackPanel stackV = new StackPanel();
TextBlock tb = new TextBlock("Header") { Foreground = Brushes.White, FontWeight = FontWeight.Bold };
stack.Children.Add(tb);
{
  StackPanel stackH = new StackPanel { Orientation = Orientation.Horizontal };
  TextBlock label = new TextBlock("Label") { Foreground = Brushes.LightGray, Width = 100.0f };
  stackH.Children.Add(label);
  Slider slider = new Slider { MinValue = 0.0f, MaxValue = 100.0f, Value = 50.0f };
  stackH.Children.Add(slider);
  stackV.Children.Add(stackH);
}
//...
We highly recommed the use of a MVVM approach, where you can expose different view model types in a collection, and then rely on DataType automatic type selection or use a DataTemplateSelector to pick the appropriate template:
public class ValueModel
{
  public string Label { get; set; }
}
public class FloatValueModel : ValueModel
{
  public float Value { get; set; }
  public float MinValue { get; set; }
  public float MaxValue { get; set; }
}
public class StringValueModel : ValueModel
{
  public string Value { get; set; }
}
public class ViewModel
{
  public string Header { get; set; }
  public ObservableCollection<ValueModel> ValueList { get; }
}
<Grid>
  <Grid.Resources>
    <DataTemplate x:Key="ValueItemTemplate">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Label}" Width="100" Foreground="LightGray"/>
        <ContentControl Content="{Binding}"/>
      </StackPanel>
    </DataTemplate>
    <DataTemplate DataType="local:FloatValueModel">
      <Slider MinValue="{Binding MinValue}" MaxValue="{Binding MaxValue}" Value="{Binding Value}"/>
    </DataTemplate>
    <DataTemplate DataType="local:StringValueModel">
      <TextBox Text="{Binding Value}"/>
    </DataTemplate>
  </Grid.Resources>
  <StackPanel>
    <TextBlock Text="{Binding Header}" Foreground="White" FontWeight="Bold"/>
    <ItemsControl ItemsSource="{Binding ValueList}" ItemTemplate="{StaticResource ValueItemTemplate}"/>
  </StackPanel>
</Grid>
Please let me know if you need more information about this.
 
StrayThought
Topic Author
Posts: 6
Joined: 18 Nov 2021, 11:41

Re: Unity/C# without XAML?

29 Feb 2024, 12:31

Thanks for the reply. I already have a data binding setup, but AFAIK it can't support grouping, group headers, collapsible sections, etc. right? Doing it in code is so much easier. Or can the data binding select item by item and use a different data template for it in a scrolling list?
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Unity/C# without XAML?

29 Feb 2024, 13:00

You can have a collection of collections if you need some kind of grouping, if that makes sense.
For example a list of categories, and then each category a list of options.
In that scenario you can even define expanders for each category to group each list of options.
 
StrayThought
Topic Author
Posts: 6
Joined: 18 Nov 2021, 11:41

Re: Unity/C# without XAML?

29 Feb 2024, 13:06

Alright, thanks. I'll stick with code only solution for now as it's cleaner overall for my use-case. Cheers.
 
User avatar
jsantos
Site Admin
Posts: 3925
Joined: 20 Jan 2012, 17:18
Contact:

Re: Unity/C# without XAML?

01 Mar 2024, 14:45

XAML is just a wrapper to code actions. Everything should be doable by code (sometimes with more steps than the markup). Also we are quite compatible with WPF, so almost everything you find on Stack Overflow or Microsoft site should almost directly translate to Noesis C#.
 
StrayThought
Topic Author
Posts: 6
Joined: 18 Nov 2021, 11:41

Re: Unity/C# without XAML?

01 Mar 2024, 14:47

Good to know, thanks Jesús.

Who is online

Users browsing this forum: Galilman and 3 guests