C# 按代码更改XAML属性

C# 按代码更改XAML属性,c#,wpf,xaml,C#,Wpf,Xaml,我有自定义组合框和自定义箭头。如何通过代码访问此箭头并更改其大小(或使其不可见)x:Name=“arrow” 我尝试过使用Product_CombBox.FindName(“箭头”)的方法,但它不起作用,所以我该如何做到这一点 <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton"> <Grid> <Grid.ColumnDefinitions>

我有自定义组合框和自定义箭头。如何通过代码访问此箭头并更改其大小(或使其不可见)x:Name=“arrow”

我尝试过使用Product_CombBox.FindName(“箭头”)的方法,但它不起作用,所以我该如何做到这一点

<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>

尝试使用
FindVisualChild
函数,如下所示:

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;
}
private void ChangePath\u单击(对象发送方,路由目标)
{
路径myPath=FindVisualChild(mymbobox,“箭头”);
//例如,更改填充颜色
if(myPath!=null)
myPath.Fill=画笔.Red;
}
私有childItem FindVisualChild(FrameworkElement obj,string elementName),其中childItem:FrameworkElement
{
如果(obj!=null)
{
for(int i=0;i
但这并不是在WPF中使用UI的最佳方式。您必须使用样式的强大功能:触发器、DataTriggers、附加行为,以便正确方便地使用UI


您只需遵守一条规则:无论查看
视图的哪一侧:更改颜色、大小等都应在查看
视图的一侧进行。及时会增长逻辑,代码会悄悄地变成意大利面代码。因此,进一步的工作和更改逻辑将很困难。

如果您的
切换按钮
组合框
的较大
模板
的一部分,那么您需要首先找到
切换按钮
,然后找到
路径。为此,您需要给
ToggleButton
一些名称,比如说
x:name=“ToggleButton”
,然后:

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

我是否正确理解您有带有自定义
模板的
组合框
,其中包括带有上面显示的
控制模板的
切换按钮
,并且您希望在代码中获得该
路径
?在什么情况下您希望更改此UI元素的属性?我很确定您需要一个带有
TargetName
DataTrigger
,而不是过程代码。