I have a user control which renders its content at run time, based on the value of a property. So the XAML designer in Visual Studio is not able to show any content of the control when it is placed in a Window. Is there any way I can help the designer, tell it to set a specific property of the control to a value?
thanks,
Here is the user control. And the Window the control is placed in. When the XAML designer shows the window the control displays as empty.
<UserControl x:Class="wpfDemo.Controls.RadioButtonControl"
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"
xmlns:local="clr-namespace:wpfDemo.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"><Grid x:Name="grid1"></Grid></UserControl>
using System.Windows;
using System.Windows.Controls;
namespace wpfDemo.Controls
{
/// <summary>
/// Interaction logic for RadioButtonControl.xaml
/// </summary>
public partial class RadioButtonControl : UserControl
{
public int NumButton
{
get { return (int)GetValue(NumButtonProperty); }
set { SetValue(NumButtonProperty, value); }
}
// Using a DependencyProperty as the backing store for NumButton. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NumButtonProperty =
DependencyProperty.Register("NumButton", typeof(int), typeof(RadioButtonControl),
new PropertyMetadata(0, new PropertyChangedCallback(OnNumButtonChanged)));
private static void OnNumButtonChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var numButton = e.NewValue as int?;
var uc = d as RadioButtonControl;
if ((uc != null) && (numButton != null))
{
uc.CreateButtons(numButton.Value);
}
}
public RadioButtonControl()
{
InitializeComponent();
}
private void CreateButtons( int NumButton )
{
grid1.Children.Clear();
var wrap1 = new WrapPanel();
grid1.Children.Add(wrap1);
for (int ix = 0; ix < NumButton; ++ix)
{
var rb = new RadioButton();
rb.Content = "Button" + ix;
rb.Name = "Button" + ix;
wrap1.Children.Add(rb);
}
}
}
}<Window x:Class="wpfDemo.steve5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpfDemo"
xmlns:uc="clr-namespace:wpfDemo.Controls"
mc:Ignorable="d"
Title="steve5" Height="300" Width="300"><StackPanel x:Name="layoutRoot" Orientation="Vertical"><uc:RadioButtonControl x:Name="control1" NumButton="{Binding NumButton}" /></StackPanel></Window>
using System.Windows;
namespace wpfDemo
{
/// <summary>
/// Interaction logic for steve5.xaml
/// </summary>
public partial class steve5 : Window
{
public steve5()
{
InitializeComponent();
this.Loaded += Steve5_Loaded;
}
public int NumButton
{ get; set; }
private void Steve5_Loaded(object sender, RoutedEventArgs e)
{
this.layoutRoot.DataContext = this;
this.NumButton = 4;
}
}
}