C
C#8mo ago
stigzler

✅ Shorten Property code

Is there a simpler way to write this:
private bool _gistEdited = false;
public bool GistEdited
{
get {
return _gistEdited;
}
set
{
_gistEdited = value;
mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255,255,0,0));
}
}
private bool _gistEdited = false;
public bool GistEdited
{
get {
return _gistEdited;
}
set
{
_gistEdited = value;
mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255,255,0,0));
}
}
I know you can do:
// No private backing memeber
public int GistEdited { get; set; }
// No private backing memeber
public int GistEdited { get; set; }
And also:
// No private backing memeber
public bool GistEdited { get => GistEdited; set => mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); }
// No private backing memeber
public bool GistEdited { get => GistEdited; set => mainWindowControl.SaveButton.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); }
But with the former, you can't do the mainWindowControl.SaveButton.BorderBrush =... and with the latter, you can't set GistEdited (as it'd result in an infinite loop)
2 Replies
leowest
leowest8mo ago
if u use Community ToolKit and a trigger Binding your GistEdited yeah it would be just
[ObservableProperty]
private bool _gistEditec;
[ObservableProperty]
private bool _gistEditec;
and CTK does all the boiler plate for u with SG Trigger example
<Button Width="200" Height="40">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding GistEdited}" Value="True">
<Setter Property="BorderBrush" Value="DarkGoldenrod"/>
<Setter Property="BorderThickness" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button Width="200" Height="40">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding GistEdited}" Value="True">
<Setter Property="BorderBrush" Value="DarkGoldenrod"/>
<Setter Property="BorderThickness" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server