C# Silverlight中的控件启用/禁用行为

C# Silverlight中的控件启用/禁用行为,c#,silverlight,controls,C#,Silverlight,Controls,我有一个应用程序,其中有很多文本框。此外,此文本框实际上从未被禁用,而是变为只读。我更喜欢对所有控件使用ContentControl的一个属性,但如果我将IsEnabled设置为false,则所有文本框都将被禁用。我如何使它们进入只读模式?我不喜欢自己控制,也许我可以用风格或其他东西来重新分配行为 编辑:我实际上在寻找一种解决方案,它允许我使用绑定通过在一个位置绑定来绑定所有控件的状态(IsReadOnly)。比如: <ContentControl IsEnabled="{Binding

我有一个应用程序,其中有很多文本框。此外,此文本框实际上从未被禁用,而是变为只读。我更喜欢对所有控件使用ContentControl的一个属性,但如果我将IsEnabled设置为false,则所有文本框都将被禁用。我如何使它们进入只读模式?我不喜欢自己控制,也许我可以用风格或其他东西来重新分配行为

编辑:我实际上在寻找一种解决方案,它允许我使用绑定通过在一个位置绑定来绑定所有控件的状态(IsReadOnly)。比如:

<ContentControl IsEnabled="{Binding boolFlag, Mode=OneWay}">
    <Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch">
        ....
        <TextBox/>
    </Grid>
</ContentControl>

....

我建议您对ContentControl使用一种简单的扩展方法,使文本框
IsReadOnly=True
。例如:

public static class ContentControlEx
{
    public static void DisableTextBoxes(this ContentControl contentControl)
    {
        FrameworkElement p = contentControl as FrameworkElement;
        var ts = p.GetChildren<TextBox>();
        ts.ForEach(a => { if (!a.IsReadOnly) a.IsReadOnly = true; });
    }

    public static List<T> GetChildren<T>(this UIElement parent) where T : UIElement
    {
        List<T> list = new List<T>();
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < count; i++) {
            UIElement child = VisualTreeHelper.GetChild(parent, i) as UIElement;
            if (child != null) {
                if (child is T)
                    list.Add(child as T);

                List<T> l1 = GetChildren<T>(child);
                foreach (T u in l1)
                    list.Add(u);
            }
        }
        return list;
    }
}
我有这样一个XAML:

<Grid x:Name="LayoutRoot" Background="White">
    <ContentControl IsEnabled="True" Name="content">
        <StackPanel Margin="15">
            <TextBox Width="150" Name="tb1" Margin="5" Text="{Binding tb1}" />
            <TextBox Width="150" Name="tb2" Margin="5" Text="{Binding tb2}" />
            <TextBox Width="150" Name="tb3" Margin="5" Text="{Binding tb3}"/>
            <TextBox Width="150" Name="tb4" Margin="5" Text="{Binding tb4}"/>
            <Button Name="bSubmit" Click="bSubmit_Click">Make Textboxes readonly</Button>
        </StackPanel>
    </ContentControl>
</Grid>

使文本框为只读

让我知道它是否有用…

我建议您对ContentControl使用一种简单的扩展方法,它将使文本框
IsReadOnly=True
。例如:

public static class ContentControlEx
{
    public static void DisableTextBoxes(this ContentControl contentControl)
    {
        FrameworkElement p = contentControl as FrameworkElement;
        var ts = p.GetChildren<TextBox>();
        ts.ForEach(a => { if (!a.IsReadOnly) a.IsReadOnly = true; });
    }

    public static List<T> GetChildren<T>(this UIElement parent) where T : UIElement
    {
        List<T> list = new List<T>();
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < count; i++) {
            UIElement child = VisualTreeHelper.GetChild(parent, i) as UIElement;
            if (child != null) {
                if (child is T)
                    list.Add(child as T);

                List<T> l1 = GetChildren<T>(child);
                foreach (T u in l1)
                    list.Add(u);
            }
        }
        return list;
    }
}
我有这样一个XAML:

<Grid x:Name="LayoutRoot" Background="White">
    <ContentControl IsEnabled="True" Name="content">
        <StackPanel Margin="15">
            <TextBox Width="150" Name="tb1" Margin="5" Text="{Binding tb1}" />
            <TextBox Width="150" Name="tb2" Margin="5" Text="{Binding tb2}" />
            <TextBox Width="150" Name="tb3" Margin="5" Text="{Binding tb3}"/>
            <TextBox Width="150" Name="tb4" Margin="5" Text="{Binding tb4}"/>
            <Button Name="bSubmit" Click="bSubmit_Click">Make Textboxes readonly</Button>
        </StackPanel>
    </ContentControl>
</Grid>

使文本框为只读

如果有帮助,请告诉我……

在您的情况下,使用DataForm控件似乎是最好的。它将允许您作为一个组控制其中的每个字段。它确实提供了IsReadOnly选项,而且还提供了许多非常好的免费功能

这段视频做了很好的介绍

查找数据表单


干杯,

对于您的情况,似乎使用DataForm控件是最好的。它将允许您作为一个组控制其中的每个字段。它确实提供了IsReadOnly选项,而且还提供了许多非常好的免费功能

这段视频做了很好的介绍

查找数据表单


干杯,

如果我理解正确,您希望将每个
文本框.IsReadOnlyProperty
绑定到一个bool

您可以尝试类似这样的操作,其方式与绑定
ContentControl
IsEnabled
属性的方式类似:

<TextBox IsReadOnly="{Binding boolFlag, Mode=OneWay}" ... /> <!-- in each of your textboxes -->


这将为您提供所需内容:更改
boolFlag
,打开或关闭每个文本框。

如果我理解正确,您希望将每个
文本框。IsReadOnlyProperty
绑定到单个bool

您可以尝试类似这样的操作,其方式与绑定
ContentControl
IsEnabled
属性的方式类似:

<TextBox IsReadOnly="{Binding boolFlag, Mode=OneWay}" ... /> <!-- in each of your textboxes -->



这应该为您提供您所需要的:更改
boolFlag
,每个文本框打开或关闭。

您可以添加一些关于如何使用单个属性的代码吗?我是否理解正确,您所有的
textbox
控件都是
ContentControl
的子控件?ContentControl只能包含一个子控件。它们可能在格子里或者其他什么的……是的,它们在格子里。Bur grid没有IsEnabled属性,因此我必须将grid放在ContentControl中。您可以添加一些关于如何使用单个属性的代码吗?我是否正确理解,您所有的
TextBox
控件都是
ContentControl
的子控件?ContentControl只能包含一个子控件。它们可能在格子里或者其他什么的……是的,它们在格子里。Bur grid没有IsEnabled属性,所以我必须将grid放在ContentControl中。我更新了我的问题,以便更准确地描述我需要什么。六羟甲基三聚氰胺六甲醚。。。我不确定我是否能够创建扩展依赖属性(()但是如果我决定创建我自己版本的ContentControl,你的帖子会很有帮助,谢谢。我更新了我的问题,以更准确地描述我需要什么。嗯……我不确定我是否能够创建扩展依赖属性(((但是如果我决定制作我自己版本的ContentControl,你的帖子会很有帮助,谢谢。我不想把粘贴绑定复制到我在表单上的每个控件!我不想把粘贴绑定复制到我在表单上的每个控件!是的,看起来像我需要的,我明天会研究的。是的,这对我的情况来说是最好的。)n、 很遗憾,我们现在没有时间将项目转换为datafoms,所以我将编写自己的内容控件(是的,看起来像是我需要的,我明天会研究它。是的,结果是对我的情况来说是最好的,所以让我们标记一个答案。不幸的是,我们现在没有时间将我们的项目转换为datafoms,所以我将编写我自己的内容控件(