Very new to WPF and .NET in general. Previously I worked with winforms and VBA, very basic stuff.
I'm trying to learn how to utilize XAML and events. I know there are better ways to handle the following scenario, but I just want to understand the basics.
Imagine I have 2 buttons, connect and disconnect. When the window loads, Connect Has imgConnect, Disconnect has imgDisconnect. All the images are static resources
xaml
<Button x:Name="btnToolbarConnect" Content ="{StaticResource imgConnect}" ToolTip="Connect" Foreground="#FFF0F0F0" IsEnabled="True"/>
<Button x:Name="btnToolbarDisconnect" Content ="{StaticResource imgDisabledDisconnect}" ToolTip="Disconnect" Foreground="#FFF0F0F0" IsEnabled="False"/>
When a user presses connect (and successfully connects), the image on connect changes to imgDisabledConnect, and Disconnect changes to imgDisabledDisconnect (vice versa)
cs
private void SystemSrv_ConnectionStateChanged(object sender, ConnectionStateChangedEventArgs e)
{
CheckServiceState();
}
private bool CheckServiceState()
{
try
{
switch (service.ReadState().State.ToString())
{
case "Connected":
return true;
case "Disconnected":
return false;
default:
return true;
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return false;
}
}
In the past, I would have addressed the UI elements (text1.text.disable
) of a winform in the CheckServiceState switch statement, but to my knowledge, this isnt best practice with XAML/WPF (nor do I think it's possible for the button content image).
I have searched around here and other sites, but the only thing I can find is buttons firing their own events and changing their own images through XAML triggers/styles.
Basically im asking, in the most basic way, how do I change the image on one button when an event happens that isnt an event of it's own creation?
Once I get this figured out, I will play with styles and enable/disable many UI elements when this event fires.
==============Edit 1=============
Ive tried this, but the button Content doesnt change. Do I have to associate the binding TriggerTest somewhere else?
<Button x:Name="btnToolbarTcStart" Content = "New" ToolTip="Re/Start TwinCAT System" IsEnabled="False" Click="btnToolbarTcStart_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding TriggerTest}" Value="True">
<Setter Property="Content" Value="{StaticResource TcStart}"/>
</DataTrigger>
<DataTrigger Binding="{Binding TriggerTest}" Value="False">
<Setter Property="Content" Value="{StaticResource TcGrey}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
CS
private bool TriggerTest(bool value)
{
Debug.WriteLine(value);
return value;
}