can I create something like this in silverlight? A box with editable title and the rest of the text
Asked
Active
Viewed 318 times
0
-
Hi, it is recommended to show some code that shows what you tried so far and where you got stuck. Such short questions are hard to answer and often receive downvotes and/or get closed quickly. (Commented after seeing your previous questions). - Oh, and please _never_ repost a question. Improve your question in stead. – Gert Arnold Jun 07 '13 at 19:15
1 Answers
4
You could create a custom user control:
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>
Code behind:
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;
}
}
}
A sample:
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>
Code behind:
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));
}
}
}
This is WPF but should be fine in Silverlight too, except the MessageBox stuff but it's only for debugging purposes...
EDIT: Here is a sample for dynamic generation:
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>
CodeBehind:
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)
});
}
}
}

Pragmateek
- 13,174
- 9
- 74
- 108
-
It gives me a error in those "DependencyProperty". And other problem is that in xaml I just can have a button to create the boxes.. the boxes can't be already created when the program starts – terrorista Jun 07 '13 at 18:20
-
1@LuisAmaro The DependencyProperty things should work if you add a `null` parameter to the end of the `Register` call. And you can create the boxes at runtime like this: `var myBox = new MyBox(); myControl.Children.Add(myBox);` where `myControl` is something that displays it (e.g. `StackPanel`, `Grid`, or `Canvas`). – Tim S. Jun 07 '13 at 18:26
-
The error is due to SL having only one overload for Register, to fix it just add a null parameter at the end. The sample only illustrates one use-case for the control, but you can use it as any native SL control. – Pragmateek Jun 07 '13 at 18:26
-
yes thanks, i tested and the box worked, but now how can I make more boxes like this just by clicking a button? because i think this is two separated textboxes in xaml (header+text) and i'm guessing I have to do it all in c# .. i'm new at silverlight btw :p – terrorista Jun 07 '13 at 18:46
-
many thanks, i can create boxes by clicking a button.. now i'm trying to drag and drop these boxes, I tried with a sample code i have from moving rectangles and other objects, but with these i cant – terrorista Jun 07 '13 at 23:57
-
You should be able to do that with the same kind of code by putting the boxes in a Canvas rather than a StackPanel. If you have any issue open a new question. – Pragmateek Jun 08 '13 at 12:57
-
but it gaves me an error saying that this is not recognized as an object – terrorista Jun 08 '13 at 15:43
-
What is not recognized? And please post a new question this will be easier for speaking and everybody will be able to help you. :) – Pragmateek Jun 08 '13 at 16:01
-
do you know how can I save the info from the textboxes to an xml file? can you look at my new question? :) http://stackoverflow.com/questions/17125665/silverlight-save-textbox-to-xml – terrorista Jun 17 '13 at 14:24