0

I am trying to get a string from my C# code over to my XAML, but I can't seem to find a way to do it

My C# code

public string demoColour= "#FFFFFF";

My XAML Code

...
<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" TargetName="Bd" Value="{ NOT SURE WHAT GOES HERE :( }"/>
</Trigger>                            
...
3
  • 3
    Take a look at data binding. Or make demoColour a static class member and use x:Static. Commented Feb 11, 2020 at 12:54
  • I seem to understand how to do it now, this article here helped: tutorialspoint.com/wpf/wpf_data_binding.htm Thanks for telling me what to look into Commented Feb 11, 2020 at 14:14
  • What is Databinding? Commented Feb 11, 2020 at 14:28

1 Answer 1

2

As Clemes said, you should take a look at data binding. Data binding is one really important thing in WPF.

But here is one Solution, which works fine:

  1. Make a new Class called ViewModel
  2. Add a Property to that Class like public string MyColor { get; set; } = "#FFFFFF";
  3. Set the DataContext in your XAML:
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
  1. Bind your Property to your XAML whereever you want. For Example:
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="{Binding MyColor}"/>
        </Style>
    </Window.Resources>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.