C
C#13mo ago
Ernoi

✅ Wpf Enum conditional rendering

Hello im new to Wpf and feel like conditional rendering is a bit clunky I have a Enum with diffrent states in my viewmodel and want to render different things like example Enum has Loading, Start, InProgress. I want to show a TextBox that displays diffrent stuff depending on the incoming Enum state. Can anyone point me to a good way of doing this or give me an example?
2 Replies
sibber
sibber13mo ago
bind the textbox text to a prop that returns
state switch
{
Loading => blabla,
etc...
};
state switch
{
Loading => blabla,
etc...
};
if you want to render a different control then use a ContentControl and bind Content
Ernoi
ErnoiOP13mo ago
<DataTemplate x:Key="DayTimeTemplate">
<Image Source="Icons/Suntransparent.png" Margin="314,7,334,7" />
</DataTemplate>
<DataTemplate x:Key="NightTimeTemplate">
<Image Source="Icons/MoonTransparent.png" Margin="317,10,338,10" />
</DataTemplate>
<DataTemplate x:Key="None">
</DataTemplate>

<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource DayTimeTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsDaytime }" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource DayTimeTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsDaytime}" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource NightTimeTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding GameTime}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource None}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<DataTemplate x:Key="DayTimeTemplate">
<Image Source="Icons/Suntransparent.png" Margin="314,7,334,7" />
</DataTemplate>
<DataTemplate x:Key="NightTimeTemplate">
<Image Source="Icons/MoonTransparent.png" Margin="317,10,338,10" />
</DataTemplate>
<DataTemplate x:Key="None">
</DataTemplate>

<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource DayTimeTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsDaytime }" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource DayTimeTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsDaytime}" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource NightTimeTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding GameTime}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource None}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
Something like that?
Want results from more Discord servers?
Add your server