-
October 14th, 2010, 09:19 PM
#1
WPF Custom Control ContentControl for a label with other items
apologize if this question seems silly or has already been asked somewhere else, but I cannot seem to find the solution.
I have a custom WPF user control 'DotControl' that consists of a dynamic number of circles (like a multiple choice question), those circles are added programatically when the DotCount property is set. This is have working wonderfully.
What i cannot seem to do is, i would like to also have a Label (or text block I'm not really sure of the difference) built into this control (so that I do not have to add a label on the main window as well).
ex:
MyLabelTextHere O O O O O
I cannot seem to make BOTH the text and the circles show up, it seems either one or the other works. Ideally what i would like to be able to do is use the Content property so that I can have my XAML look something like this:
<localotControl Name="MyDotControl" DotCount="5" >MyLabelTextHere</localotControl>
Does anyone know how to accomplish this? Any help or push in the right direction would be greatly appreciated.
Thanks,
Kurt
Sample code:
<pre>
<UserControl x:Class="MyProject.UserControls.DotControl"
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="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
designHeight="30" designWidth="304"
HorizontalAlignment="Left" VerticalAlignment="Top">
<StackPanel Name="DotPanel" Orientation="Horizontal" Width="Auto" Height="Auto">
</StackPanel>
</UserControl>
public partial class DotControl : UserControl
{
private int _dotCount;
private List<Ellipse> _dots;
public DotControl()
{
InitializeComponent();
_dots = new List<Ellipse>();
}
public int DotCount
{
get
{
return _dotCount;
}
set
{
_dotCount = value;
refreshDots();
}
}
private void refreshDots()
{
clearDots();
for (int i = 0; i < _dotCount; i++)
{
Ellipse e = new Ellipse();
e.Height = 15;
e.Width = 15;
e.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
e.Fill = Brushes.White;
e.Stroke = Brushes.Black;
e.StrokeThickness = 1.0f;
e.Margin = new Thickness(5, 0, 0, 0);
e.MouseUp += new MouseButtonEventHandler(dot_MouseUp);
this.addDot(e);
}
}
private void clearDots()
{
foreach (Ellipse e in _dots)
DotPanel.Children.Remove(e);
_dots.Clear();
}
private void addDot(Ellipse e)
{
this._dots.Add(e);
this.DotPanel.Children.Add(e);
}
}
</pre>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|