- StrayThought
- Posts: 6
- Joined:
Unity/C# without XAML?
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.
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.
-
sfernandez
Site Admin
- Posts: 3005
- Joined:
Re: Unity/C# without XAML?
Although it is possible just to create UI elements in code:
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:
Please let me know if you need more information about this.
Code: Select all
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);
}
//...
Code: Select all
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; }
}
Code: Select all
<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>
- StrayThought
- Posts: 6
- Joined:
Re: Unity/C# without XAML?
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?
-
sfernandez
Site Admin
- Posts: 3005
- Joined:
Re: Unity/C# without XAML?
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.
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
- Posts: 6
- Joined:
Re: Unity/C# without XAML?
Alright, thanks. I'll stick with code only solution for now as it's cleaner overall for my use-case. Cheers.
Re: Unity/C# without XAML?
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#.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests