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,但我更希望它更安全。