繁体   English   中英

让控件动态显示在WPF Form C#VS 2010中

[英]Let Controls Appear dynamically in WPF Form C# VS 2010

我的应用程序上有一个UserInput Form,其中包含大量的文本框和数据网格。 有些客户端不会每次都使用它们。因此,我希望某些客户端在用户单击按钮或复选框后动态显示。

我尝试过在线寻求帮助,但是没有运气。

预先感谢您的任何建议

1)第一个基本问题-您是否需要大量的文本框和数据网格? 听起来这可能会导致混乱的用户界面。 我总是会首先尝试简化用户界面。

2)控件必须固定布置吗? 它像打开和关闭控件的可见性一样简单吗?

3)如果控件布局必须是动态的,则可以将控件“ .Add(new Button(...))”动态添加到列表/网格。 除了一些简单的更改之外,我不确定是否会推荐这种方法。

4)是否有所有客户共有的控件?

5)如果有通用控件,则可以考虑使用UserControl对它们进行分组,并使用ContentPresenter或其他控件将它们动态添加到表单中(确切的实现方法可能取决于您是否使用MVVM体系结构)。 您可以将contentpresenter绑定到作为UserControl的属性。

6)最坏的情况是每个客户端的布局都必须如此灵活,以至于您只需要为每个客户端创建一个新的UserControl。 绑定到的基础数据对象可以保持不变,但是通过每个UserControl公开的方面却有所不同。

您对WPF的经验如何? 要真正有效地执行此类操作,您需要研究数据绑定,视图,内容演示者,列表/网格,UserControl。

由于您刚开始使用WPF,因此我想向您展示一些示例,这些示例可以对触发器等其他内容有所了解,这可能会对您有所帮助。

<Window x:Class="WpfApplication1.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>

        <ToggleButton x:Name="tgbtnTextBoxVisibility" Click="tgbtnTextBoxVisibility_Click" >
            <!-- Define the Style Trigger, to toggle the Text on the Toggle Button. If the ToggleButton is unchecked then display 'Hide TextBoxes'-->
            <!---Do not set the 'Content' property directly on the control, which overrides the 'Content' defined in Styles/Triggers, because of property precedence-->
            <ToggleButton.Style>
                <Style TargetType="ToggleButton">
                    <Setter Property="Content" Value="Show TextBoxes"/>
                    <Style.Triggers>
                        <Trigger Property="IsChecked"   Value="True" >
                            <Setter Property="Content" Value="Hide TextBoxes"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>

        <!-- When the StackPanel below is shown, you can observe the 'Show TextBlocks' button automatically slides down-->
        <StackPanel x:Name="spTextBoxes" Visibility="Collapsed">
            <TextBox x:Name="txtName" Width="100" Height="30"/>
            <TextBox x:Name="txtCompany" Width="100" Height="30"/>
        </StackPanel>

        <ToggleButton x:Name="tgbtnTextBlockVisibility" Click="tgbtnTextBlockVisibility_Click">
            <ToggleButton.Style>
                <Style TargetType="ToggleButton">
                    <Setter Property="Content" Value="Show TextBlocks"/>
                    <Style.Triggers>
                        <Trigger Property="IsChecked"   Value="True" >
                            <Setter Property="Content" Value="Hide TextBlocks"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>
        <StackPanel x:Name="spTextBlocks" Visibility="Collapsed">
            <!--'Text' property of TextBlock is directly bound to the corresponding TextBox's 'Text' properrty, which changes as you type in the text into source TextBox-->
            <TextBlock x:Name="tbkName" Text="{Binding ElementName=txtName,Path=Text}"  Width="100" Height="30"/>
            <TextBlock x:Name="tbkCompany" Text="{Binding ElementName=txtCompany,Path=Text}" Width="100" Height="30"/>
        </StackPanel>

    </StackPanel>
</Window>

在后面的代码中,我设置相应堆栈面板的可见性。 注意:使用ValueConverter更改可见性可能更合适。

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void tgbtnTextBoxVisibility_Click(object sender, RoutedEventArgs e)
        {
            ToggleButton tgbtnTextBoxes=sender as ToggleButton;

            if (tgbtnTextBoxes.IsChecked.HasValue && tgbtnTextBoxes.IsChecked.Value)
                spTextBoxes.Visibility = Visibility.Visible;
            else
                spTextBoxes.Visibility = Visibility.Collapsed; //Note: Visibility.Collapsed will resize the layout where as Visibility.Hidden will not.
        }

        private void tgbtnTextBlockVisibility_Click(object sender, RoutedEventArgs e)
        {
            ToggleButton tgbtnTextBlocks = sender as ToggleButton;

            if (tgbtnTextBlocks.IsChecked.HasValue && tgbtnTextBlocks.IsChecked.Value)
                spTextBlocks.Visibility = Visibility.Visible;
            else
                spTextBlocks.Visibility = Visibility.Collapsed; //Note: Visibility.Collapsed will resize the layout where as Visibility.Hidden will not.
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM