简体   繁体   English

如何从代码隐藏更改 DataTemplate 中特定元素的属性?

[英]How to change a property of a specific element in a DataTemplate from code behind?

I have a bug in my code.我的代码中有一个错误。 FindChild<T>() is searching a Element which is named "PasswordTextBox" but because "PasswordTextBox" is in the DataTemplate there are many TextBoxes. FindChild<T>()正在搜索一个名为“PasswordTextBox”的元素,但因为“PasswordTextBox”在 DataTemplate 中,所以有很多文本框。 And if I will click on the eye (the mah:FontIcon) in the second row or third row or whatever it even changes the FontFamily of the first TextBox in the DataTemplate.如果我单击第二行或第三行中的眼睛(mah:FontIcon)或其他任何内容,它甚至会更改 DataTemplate 中第一个 TextBox 的 FontFamily。

How can I change the TextBox.FontFamily of the element in the row of the DataTemplate where the eye is clicked?如何更改单击眼睛的 DataTemplate 行中元素的 TextBox.FontFamily?

That is the xaml code:即xaml代码:

<HeaderedItemsControl Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1">
    <ItemsControl x:Name="ListViewItems">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Margin="1 10 0 10" Height="60">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="1" 
                                 x:Name="PasswordTextBox"
                                 Text="{Binding Password, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                 Height="40" 
                                 VerticalAlignment="Center"
                                 VerticalContentAlignment="Center" 
                                 HorizontalContentAlignment="Left"
                                 HorizontalAlignment="Stretch" 
                                 FontFamily="pack://application:,,,/Fonts/#password"
                                 Margin="-25 0 100 0"
                                 Padding="6, 4, 45, 0">
                        </TextBox>
                        <mah:FontIcon Grid.Column="1" 
                                      FontFamily="Segoe MDL2 Assets"
                                      HorizontalAlignment="Right"
                                      VerticalAlignment="Center"
                                      FontSize="28"
                                      Glyph="&#xF78D;"
                                      Margin="0, 0, 110, 0"
                                      Foreground="#cfcfcf"
                                      MouseLeftButtonUp="OnEyeClicked" />
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</HeaderedItemsControl>

And that is my c# code behind:那是我后面的 c# 代码:

   private void OnEyeClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        TextBox foundTextBox = FindChild<TextBox>(Application.Current.MainWindow, "PasswordTextBox");
        if (ShowPassword)
        {
            foundTextBox.FontFamily = new FontFamily("Segoe MDL2");
            foundTextBox.Padding = new Thickness(6, 4, 45, 0);
            ShowPassword = false;
        }
        else
        {
            foundTextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");
            foundTextBox.Padding = new Thickness(6, 0, 45, 0);
            ShowPassword = true;
        }
    }
    
    public static T FindChild<T>(DependencyObject parent, string childName)
        where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        if (parent == null) return null;
    
        T foundChild = null;
    
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);
    
                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's name is of the request name
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }

you can search within template bounds, relative to sender element:您可以在模板范围内搜索,相对于sender元素:

private void OnEyeClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var ico = (FontIcon)sender;
    var grid = (Grid)ico.Parent;
    TextBox foundTextBox = FindChild<TextBox>(grid, "PasswordTextBox");
    if (ShowPassword)
    {
        foundTextBox.FontFamily = new FontFamily("Segoe MDL2");
        foundTextBox.Padding = new Thickness(6, 4, 45, 0);
        ShowPassword = false;
    }
    else
    {
        foundTextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");
        foundTextBox.Padding = new Thickness(6, 0, 45, 0);
        ShowPassword = true;
    }
}

btw, ShowPassword should also be relative to each password box, shouldn't it?顺便说一句,ShowPassword 也应该与每个密码框相关,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM