Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Set UserControl's dependency property during design-time

$
0
0

Hi,

Is there a way to set the design-time value of a UserControl's dependency property? Not when I'm using this UserControl, on a Page, or something but when I actually designing the control. (I would prefer not set the property's default value, for design data)

Here is a sample code:

public partial class MyControl : UserControl
{public string MyProperty
	{get { return (string)GetValue(MyPropertyProperty); }set { SetValue(MyPropertyProperty, value); }
	}public static readonly DependencyProperty MyPropertyProperty =DependencyProperty.Register("MyProperty"typeof(string), typeof(MyControl), new PropertyMetadata("", MyPropertyChanged));private static void MyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{// do some parsing magic here, and distribute the results into the controls
		(d as MyControl).tb1.Text = res1;
		(d as MyControl).tb2.Text = res2;
		(d as MyControl).tb3.Text = res3;
	}public MyControl()
	{
		InitializeComponent();if (WpfHelper.IsInDesignMode)
		{
			MyProperty = "Some complex string data...";	// this isn't works on the UserControl's design view
		}
	}
}

And the XAML file

<UserControl x:Class="st.Reader.UI.Controls.MyControl"             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"              MyProperty="some design data" (<- I would like to do something like this)             d:DesignHeight="300" d:DesignWidth="300">    <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="*"/>            <RowDefinition Height="*"/>            <RowDefinition Height="*"/>        </Grid.RowDefinitions>        <TextBlock x:Name="tb1" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top"><Run Text="TextBlock"/></TextBlock>        <TextBlock x:Name="tb2" TextWrapping="Wrap" Grid.Row="1"><Run Text="TextBlock"/></TextBlock>        <TextBlock x:Name="tb3" TextWrapping="Wrap" Text="TextBlock" Grid.Row="2"/>    </Grid></UserControl>

Thx


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>