簡體   English   中英

如何從代碼隱藏更改 DataTemplate 中特定元素的屬性?

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

我的代碼中有一個錯誤。 FindChild<T>()正在搜索一個名為“PasswordTextBox”的元素,但因為“PasswordTextBox”在 DataTemplate 中,所以有很多文本框。 如果我單擊第二行或第三行中的眼睛(mah:FontIcon)或其他任何內容,它甚至會更改 DataTemplate 中第一個 TextBox 的 FontFamily。

如何更改單擊眼睛的 DataTemplate 行中元素的 TextBox.FontFamily?

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

那是我后面的 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;
    }

您可以在模板范圍內搜索,相對於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;
    }
}

順便說一句,ShowPassword 也應該與每個密碼框相關,不是嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM