Changing XAML property by code

I have custom combobox with custom arrow. How can I access this arrow via code and change its size (or make it invisible) x:Name="Arrow";

I have tried something with Product_CombBox.FindName("Arrow") but it did not work, so how do I do this?

<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="MouseOver"/>
                <VisualState x:Name="Pressed"/>
                <VisualState x:Name="Disabled"/>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Border x:Name="Border" SnapsToDevicePixels="True" Background="White" BorderBrush="Black" CornerRadius="15" BorderThickness="1"  />
        <Border x:Name="Border2" SnapsToDevicePixels="True" Background="White" BorderBrush="Black" CornerRadius="15" BorderThickness="1" />
        <Path x:Name="Arrow" Data="M 0 0 L 8 12 L 16 0 Z" Fill="Black" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
        </Trigger>

        <Trigger Property="ToggleButton.IsChecked" Value="true">
        </Trigger>
        <!-- <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{StaticResource ForegroundDisabledBrush}" />
        </Trigger> -->
        <DataTrigger Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" Value="True">
        </DataTrigger >
    </ControlTemplate.Triggers>
</ControlTemplate>

Answers


If your ToggleButton is part of larger Template for ComboBox then you you'll need to first find that ToggleButton and then find that Path. For that you need to give ToggleButton some name, let's say x:Name="ToggleButton" and then:

var button = myComboBox.Template.FindName("ToggleButton", myComboBox) as ToggleButton;
var path = button.Template.FindName("Arrow", button) as System.Windows.Shapes.Path;

Try using FindVisualChild<T> function, like this:

private void ChangePath_Click(object sender, RoutedEventArgs e)
{
    Path myPath = FindVisualChild<Path>(MyComboBox, "Arrow");

    // For example, change fill color
    if (myPath != null)
        myPath.Fill = Brushes.Red;
}

private childItem FindVisualChild<childItem>(FrameworkElement obj, string elementName) where childItem : FrameworkElement
{
    if (obj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            var child = VisualTreeHelper.GetChild(obj, i);

            if (child != null && child is childItem && (child as childItem).Name == elementName)
                return child as childItem;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child as FrameworkElement, elementName);

                if (childOfChild != null)
                    return childOfChild;
            }
        }
    }

    return null;
}

But this is not the best way to work with UI in WPF. You must use the power of styles: Triggers, DataTriggers, attached behavior in order to work correctly and conveniently with UI.

You just have to stick to one rule: whatever side on View: change colors, sizes, etc. should be done on the side View. Just in time will grow logic and code are quietly going to turn into spaghetti code. Accordingly, further work and changing logic will be difficult.


Need Your Help

target process crash when using remoteThread to inject a function

delphi

I'm using this code to inject my function, but it causes the target process to crash. Does anyone know why?

Regex for Network Engineers - Find the context given by a string between two delimiters

regex windows

I am a network engineer with limited programming skills, I use a tool called dnGREP that is based in NET4.0 for searching text within common text files. What I need to do as part of my job is finding