我正在使用以下内容处理 WPF 页面:
<ItemsControl ItemsSource="{Binding Peopl.PhoneNums}" x:Name="PhoneList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" x:Name="PhoneEntry">
<TextBlock Text="123-456-78901"/>
<ComboBox ...>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
可以有多个堆栈面板,每个都有一个唯一的电话号码;在代码隐藏中,每个电话号码都有一个标志,指示是否应启用它;我希望能够根据该标志启用堆栈面板中的每个条目,但我无法访问它....
我有:
foreach (Phone phone in PhoneList.ItemsSource)
{
if (phone.ShouldBeDisabled)
{
int index = PhoneList.Items.IndexOf(phone);
PhoneList.IsEnabled = false;
//this disables the entire control;
// I can't access "PhoneEntry" here... hmm
}
}
有没有办法一次只禁用一个条目?如何访问 PhoneEntry?我是否应该尝试根据绑定(bind)值禁用每个堆栈面板条目?
请您参考如下方法:
您可以反转您的 View 模型属性并将其命名为 ShouldBeEnabled .现在您可以绑定(bind) StackPanel 的 IsEnabled属性(property)。
<StackPanel ... IsEnabled="{Binding ShouldBeEnabled}">
...
</StackPanel>
如果你不能改变 View 模型,你可以使用一个绑定(bind)转换器来反转属性值:
<StackPanel ... IsEnabled="{Binding ShouldBeDisabled,
Converter={StaticResource InverseBooleanConverter}}">
...
</StackPanel>
您的 Phone 类必须实现 INotifyPropertyChanged接口(interface)并启动 PropertyChanged ShouldBeDisabled 的值时的事件属性变化。
