【问题标题】:Create box with title and text in silverlight在silverlight中创建带有标题和文本的框
【发布时间】:2013-06-07 17:34:54
【问题描述】:

我可以在 Silverlight 中创建类似的东西吗?带有可编辑标题和其余文本的框

http://docs.jboss.org/seam/3/latest/reference/en-US/html/images/remoting-model-customer-address-uml.png

【问题讨论】:

  • 嗨,建议显示一些代码,以显示您到目前为止尝试的内容以及卡住的位置。如此简短的问题很难回答,并且经常收到反对票和/或很快被关闭。 (在看到您之前的问题后发表评论)。 - 哦,请从不重新发布问题。改为改进您的问题。

标签: c# silverlight entity case


【解决方案1】:

您可以创建自定义用户控件:

XAML:

<UserControl x:Class="WpfApplication1.MyBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding Header,UpdateSourceTrigger=PropertyChanged}"
                 BorderBrush="Black" BorderThickness="1" Margin="2" />
        <TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}"
                 TextWrapping="Wrap"
                 VerticalScrollBarVisibility="Auto"
                 AcceptsReturn="True"
                 BorderBrush="Black" BorderThickness="1" Margin="2" Grid.Row="1" />
    </Grid>
</UserControl>

后面的代码:

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class MyBox : UserControl
    {
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(MyBox));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Content", typeof(string), typeof(MyBox));

        public string Header
        {
            get { return GetValue(HeaderProperty) as string; }
            set { SetValue(HeaderProperty, value); }
        }

        public string Text
        {
            get { return GetValue(TextProperty) as string; }
            set { SetValue(TextProperty, value); }
        }

        public MyBox()
        {
            InitializeComponent();

            this.DataContext = this;
        }
    }
}

一个样本:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <local:MyBox x:Name="box1" Header="Type a header..." Text="Type a content..." Grid.Row="0" Grid.Column="0" BorderBrush="Black" BorderThickness="1" Margin="10" />
        <local:MyBox x:Name="box2" Header="Type a header..." Text="Type a content..." Grid.Row="0" Grid.Column="1" BorderBrush="Black" BorderThickness="1" Margin="10" />
        <Button Content="Show" Grid.Row="1" Grid.Column="0" Click="Button_Click" />
    </Grid>
</Window>

后面的代码:

using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format("{0}\n{1}\n\n{2}\n{3}", box1.Header, box1.Text, box2.Header, box2.Text));
        }
    }
}

这是 WPF 但在 Silverlight 中也应该没问题,除了 MessageBox 的东西,但它仅用于调试目的...

编辑: 这是动态生成的示例:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <StackPanel x:Name="panel" Orientation="Horizontal">
        <local:MyBox x:Name="box1" Header="Type a header..." Text="Type a content..." Grid.Row="0" Grid.Column="0" BorderBrush="Black" BorderThickness="1" Margin="10" />
        </StackPanel>
        <Button Content="Add" Grid.Row="1" Grid.Column="0" Click="Button_Click" />
    </Grid>
</Window>

代码隐藏:

using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            panel.Children.Add(new MyBox
            {
                Header = "Another box",
                Text = "...",
                BorderBrush = Brushes.Black,
                BorderThickness = new Thickness(1),
                Margin = new Thickness(10)
            });
        }
    }
}

【讨论】:

  • 它在那些“DependencyProperty”中给了我一个错误。另一个问题是,在 xaml 中我只能有一个按钮来创建框.. 程序启动时还不能创建框
  • @LuisAmaro 如果您在 Register 调用的末尾添加一个 null 参数,那么 DependencyProperty 应该会起作用。您可以像这样在运行时创建这些框:var myBox = new MyBox(); myControl.Children.Add(myBox); 其中myControl 是显示它的东西(例如StackPanelGridCanvas)。
  • 该错误是由于SL只有一个Register重载,修复它只需在最后添加一个空参数。该示例仅说明了该控件的一个用例,但您可以将其用作任何本机 SL 控件。
  • 是的,谢谢,我测试过并且盒子可以工作,但是现在我怎样才能通过点击一个按钮来制作更多这样的盒子?因为我认为这是 xaml 中的两个单独的文本框(标题+文本),我猜我必须在 c# 中完成这一切.. 我是 silverlight 的新手:p
  • 非常感谢,我可以通过单击按钮来创建框。现在我正在尝试拖放这些框,我尝试使用来自移动矩形和其他对象的示例代码,但使用这些我不能
猜你喜欢
  • 2011-11-03
  • 2013-02-16
  • 2016-08-15
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
相关资源
最近更新 更多