[WPF] how to create custom control that have multiple state, each state is completely different from others

Trọng Tín Võ 20 Reputation points
2023-12-26T07:31:27.27+00:00

Hello experts! Please help me on creating custom control with multiple appearances, each appearance is complete different from others (each appearance combining of different components, not just slightly change of style).

I'm making a custom control called BudgetCircleControl, that display a circle and an arc, that present the current value with max value of budget. Let me describe what I currently have.

The code is so long that I put it in a github repository, please take a look:
https://github.com/bananaback/LearnHowToMakeCustomControl
I created BudgetCircleControl that implement Control, define my custom control style in BudgetCircleControl.xaml, it all in Components folder.

This works fine but when I want to have some improvements like when CurrentValue exceed MaxValue, don't show the circle anymore, show text and other things to warning. I tried to use VisualStateManager but it just good for slightly change in style in my opinion. In my case I need complex combination of components, completely different. I try to implement something like ControlTemplate, DataTemplate to dynamically switch template in code, I also try ControlTemplate Trigger but that didn't work and really messy so I think I shouldn't put it here. I need your help. Please show me how to achieve that please, create custom control with different appearances depend on some property. Thank you so much.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,808 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,189 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Trọng Tín Võ 20 Reputation points
    2023-12-26T10:57:02.31+00:00

    I just found the solution for my case. I get the resource dictionary and access the template through the key. And it work well. Thanks for your help.

    private void ApplyTemplate()
    {
        var myResourceDictionary = new ResourceDictionary();
        myResourceDictionary.Source =
            new Uri("/Components/BudgetCircleControl.xaml",
                    UriKind.RelativeOrAbsolute);
        if (CurrentValue > MaxValue)
        {
    
            // Apply WarningTemplate
            Template = myResourceDictionary["WarningTemplate"] as ControlTemplate;
        }
        else
        {
            // Apply NormalTemplate
            Template = myResourceDictionary["NormalTemplate"] as ControlTemplate;
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.