【发布时间】:2016-05-10 00:12:08
【问题描述】:
我有一个应用程序,其中包含在某些窗口中使用的多个用户控件。这些用户控件之一定义此窗口中的所有其他用户控件是否应允许编辑,因此将所有CheckBoxes、ComboBoxes 和Buttons 的IsEnabled 属性设置为False。但是,TextBoxes 应该允许复制他们的文本,因此不应该被禁用,而只能是只读的。
我尝试遍历LogicalTree,但是一些自建的usercontrol没有任何属性可以禁用它们,但是这个usercontrol中包含的控件只有按钮和文本框。这就是为什么我尝试将样式应用于所有可变元素(CheckBox、ComboBox、Button 和 TextBox),但它不起作用。
在用户控件的Ressources 部分我定义了一些样式:
<Style TargetType="Control" x:Key="disabledStyle">
<Setter Property="IsEnabled" Value="False" />
</Style>
<Style TargetType="TextBox" x:Key="readOnlyStyle">
<Setter Property="IsReadOnly" Value="True" />
</Style>
在 CodeBehind 中,检查条件后,我尝试了以下操作:
# windowOwner is the root window containing this usercontrol
for control in [Button, ComboBox, CheckBox]:
if self.windowOwner.Resources.Contains(control):
self.windowOwner.Resources.Remove(control)
self.windowOwner.Resources.Add(control, self.Resources['disabledStyle'])
if self.windowOwner.Resources.Contains(TextBox):
self.windowOwner.Resources.Remove(TextBox)
self.windowOwner.Resources.Add(TextBox, self.Resources['readOnlyStyle'])
但是什么也没发生。我究竟做错了什么?我应该采取不同的做法吗?
=编辑1============================================= =======================
我现在尝试了以下 XAML:
<Style x:Key="disabledStyle">
<!--<Setter Property="Button.IsEnabled" Value="False" />
<Setter Property="CheckBox.IsEnabled" Value="False" />-->
<Setter Property="ComboBox.IsEnabled" Value="False" />
<Setter Property="TextBox.IsReadOnly" Value="True" />
</Style>
代码隐藏:
self.windowOwner.Style = self.Resources['disabledStyle']
令人惊讶的是,即使 IsEnabled 属性仅设置为 ComboBox,一切都被禁用。如果我只设置 TextBox.IsReadOnly 属性,则不会发生任何事情。有人可以解释一下吗?
=编辑2============================================= =======================
我现在也尝试使用转换器:
(XAML)
<Style TargetType="Control" x:Key="disabledStyle">
<Setter Property="IsEnabled" Value="False" />
<!--<Setter Property="Button.IsEnabled" Value="False" />
<Setter Property="CheckBox.IsEnabled" Value="False" />
<Setter Property="ComboBox.IsEnabled" Value="False" /> -->
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource typeConverter}}" Value="True">
<Setter Property="IsEnabled" Value="True" />
<Setter Property="TextBox.IsReadOnly" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
(转换器)
public class TypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool res = value.GetType() == typeof(TextBox);
return res;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ // Don't need any convert back
return null;
}
}
但同样,一切都被禁用(或者如果您使用注释掉的变体,则不会发生任何事情)。
我让它遍历可视化树:
visited = set()
def disableControls(control):
visited.add(control)
try:
for childNumber in xrange(VisualTreeHelper.GetChildrenCount(control)):
child = VisualTreeHelper.GetChild(control, childNumber)
if hasattr(child, 'Content') and child.Content not in visited:
disableControls(child.Content)
if type(child) in [Button, ComboBox, CheckBox]:
child.IsEnabled = False
elif type(child) == TextBox:
child.IsReadOnly = True
elif child not in visited:
disableControls(child)
except:
pass
disableControls(self.windowOwner)
但我也希望以后能够将更改重置为原始状态。这意味着我必须保存所有更改,这使得这比应有的复杂得多。我没有想法。
【问题讨论】:
-
我的回答对您有帮助吗?还是您遇到了其他问题?
标签: c# wpf xaml user-controls ironpython