Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从XAML中定义的ListBoxItems的ListBox中的PasswordBox获取密码_C#_.net_Wpf_Xaml_Listbox - Fatal编程技术网

C# 从XAML中定义的ListBoxItems的ListBox中的PasswordBox获取密码

C# 从XAML中定义的ListBoxItems的ListBox中的PasswordBox获取密码,c#,.net,wpf,xaml,listbox,C#,.net,Wpf,Xaml,Listbox,我有一个WPFListBox,每个ListBoxItem都有许多ui元素。其中一个ui元素是PasswordBox。这些ui元素在XamlControlTemplate中定义 我需要做的是从代码隐藏中的PasswordBox获取Password字符串。但是我不能绑定PasswordBox的Password属性,这不是一个动态属性,因为Password不应该长时间存储在内存中 现在我想到的解决方案是在代码隐藏中获取ListBoxItem的UIElements,然后从那里获取Password 但是我

我有一个
WPF
ListBox
,每个
ListBoxItem
都有许多
ui元素。其中一个
ui元素是
PasswordBox
。这些
ui元素
Xaml
ControlTemplate
中定义

我需要做的是从代码隐藏中的
PasswordBox
获取
Password
字符串。但是我不能绑定
PasswordBox
Password
属性,这不是一个动态属性,因为
Password
不应该长时间存储在内存中

现在我想到的解决方案是在代码隐藏中获取
ListBoxItem
UIElements
,然后从那里获取
Password


但是我不知道如何从列表框中获取代码隐藏中的
UIElements

密码框的密码属性不是依赖属性,因此无法绑定此属性

您可以使用以下任一方法从passwordbox控件获取密码

  • 将密码框控件作为参数传递给随项附加的命令

    XAML

    <Window x:Class="WpfApplication4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <StackPanel>
        <PasswordBox Name="txtPassword" VerticalAlignment="Top" Width="120" />
        <Button Content="Ok" Command="{Binding Path=OkCommand}" CommandParameter="{Binding ElementName=txtPassword}"/>
    </StackPanel>
    

    有关详细信息,请参阅“”。

    密码框的密码属性不是依赖属性,因此无法绑定此属性

    您可以使用以下任一方法从passwordbox控件获取密码

  • 将密码框控件作为参数传递给随项附加的命令

    XAML

    <Window x:Class="WpfApplication4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <StackPanel>
        <PasswordBox Name="txtPassword" VerticalAlignment="Top" Width="120" />
        <Button Content="Ok" Command="{Binding Path=OkCommand}" CommandParameter="{Binding ElementName=txtPassword}"/>
    </StackPanel>
    

    有关详细信息,请参阅“”。

    密码框的密码属性不是依赖属性,因此无法绑定此属性

    您可以使用以下任一方法从passwordbox控件获取密码

  • 将密码框控件作为参数传递给随项附加的命令

    XAML

    <Window x:Class="WpfApplication4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <StackPanel>
        <PasswordBox Name="txtPassword" VerticalAlignment="Top" Width="120" />
        <Button Content="Ok" Command="{Binding Path=OkCommand}" CommandParameter="{Binding ElementName=txtPassword}"/>
    </StackPanel>
    

    有关详细信息,请参阅“”。

    密码框的密码属性不是依赖属性,因此无法绑定此属性

    您可以使用以下任一方法从passwordbox控件获取密码

  • 将密码框控件作为参数传递给随项附加的命令

    XAML

    <Window x:Class="WpfApplication4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <StackPanel>
        <PasswordBox Name="txtPassword" VerticalAlignment="Top" Width="120" />
        <Button Content="Ok" Command="{Binding Path=OkCommand}" CommandParameter="{Binding ElementName=txtPassword}"/>
    </StackPanel>
    

    您可以参考“”以了解详细信息。

    使用了第一个解决方案,我知道它违反了MVVM,但我更希望它更安全。使用了第一个解决方案,我知道它违反了MVVM,但我更希望它更安全。使用了第一个解决方案,我知道它违反了MVVM,但我更希望它更安全。使用了第一个解决方案,我知道它违反了MVVM,但我更希望它更安全。