Sponsor
Sponsor

Reputation: 375

How i can change template before apply to control?

I need create a template and apply this to control. I have the template:

private string template = "<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Key=\"simpleSlider\" TargetType=\"{x:Type Slider}\">"
         + "<Border SnapsToDevicePixels=\"true\" BorderBrush=\"{TemplateBinding BorderBrush}\" BorderThickness=\"{TemplateBinding BorderThickness}\">"
            + "<Grid>"
                + "<Grid.RowDefinitions>"
                    + "<RowDefinition Height=\"Auto\"/>"
                    + "<RowDefinition Height=\"Auto\" MinHeight=\"{TemplateBinding MinHeight}\"/>"
                    + "<RowDefinition Height=\"Auto\"/>"
                + "</Grid.RowDefinitions>"
                + "<Rectangle x:Name=\"PART_SelectionRange\"/>"
                + "<Track x:Name=\"PART_Track\" Grid.Row=\"1\">"
                    + "<Track.Thumb>"
                        + "<Thumb x:Name=\"Thumb\">"
                            + "<Thumb.Template>"
                                + "<ControlTemplate TargetType=\"Thumb\">"
                                    + "<Grid Name=\"grid\">"
                                        + "<Rectangle x:Name=\"slideRec\" Fill=\"Red\" Stroke=\"Black\" StrokeThickness=\"1\" Width=\"10\" Height=\"18\" SnapsToDevicePixels=\"True\"/>"
                                        + "<Label Content=\"{Binding Tag}\" Height=\"16\"/>"
                                    + "</Grid>"
                                + "</ControlTemplate>"
                            + "</Thumb.Template>"
                        + "</Thumb>"
                    + "</Track.Thumb>"
                + "</Track>"
            + "</Grid>"
        + "</Border>"
    + "</ControlTemplate>";

And i need programmatically create a control and apply the template to control. The code is:

        Slider slider = new Slider();
        slider.ValueChanged += slider_ValueChanged;
        slider.Tag = sliders.Count == 0 ? 0 : calcularPosicao(sliders, result);
        slider.Template = (ControlTemplate)XamlReader.Parse(this.template);
        slider.ApplyTemplate();

How can i change the color fill of rectangle? To try find de rectangle in template I try this:

var track = (Track)slider.Template.FindName("PART_Track", slider);

But the result is null.

Upvotes: 0

Views: 145

Answers (1)

TYY
TYY

Reputation: 2716

Based on what you are trying to do. The problem is that at the time you get to track/ thumb control the control template that you have added has not been applied, to do what you are trying to do you would have to call applytemplate() to get the rectangle you are looking for.

 var slider = new Slider();
 slider.Template = (ControlTemplate)XamlReader.Parse(template);
 slider.ApplyTemplate();
 var track = (Track)slider.Template.FindName("PART_Track", slider);
 var thumb = (Thumb)(track.FindName("Thumb") );
 thumb.ApplyTemplate(); // key here
 var rect = (Rectangle)thumb.Template.FindName("slideRec", thumb);
 rect.Fill = new SolidColorBrush(Colors.Blue);

Upvotes: 1

Related Questions