I have a UserControl where I'd like to have some parameters of customization "radius"(double) and "contentSource" (string[]).
My UserControl is composed of a few nested controls:
<UserControl ...>
<Grid>
<my:Menu ...>
<my:Button>
</my:Button>
<my:Button>
</my:Button>
<my:Button>
</my:Button&g开发者_运维问答t;
</my:Menu ...>
</Grid>
I'm trying to expose the parameters with:
public double Rad
{
get { return (double)GetValue(RadProperty); }
set { SetValue(RadProperty, value); }
}
public static readonly DependencyProperty RadProperty =
DependencyProperty.Register(
"Radius",
typeof(double),
typeof(Menu));
public String[] DataSource
{
get { return (String[])GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register(
"DataSource",
typeof(String[]),
typeof(Menu));
However, there seems to be two problems, the "string[]" parameter seems to be causing crashes, but mostly, I cannot set the "Radius" property at all. Is there something else I need to do to expose the parameter?
How are you trying to access the values? I have copied your code into a UserControl and it seems to work fine. Have you set the DataContext for the object you are accessing these values from?
Here is my test code, which might help:
public partial class uc : UserControl
{
public uc()
{
InitializeComponent();
this.DataContext = this;
this.DataSource = new string[] { "hello","There" };
this.Rad = 7;
}
public String[] DataSource
{
get { return (String[])GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register(
"DataSource",
typeof(String[]),
typeof(uc));
public double Rad
{
get { return (double)GetValue(RadProperty); }
set { SetValue(RadProperty, value); }
}
public static readonly DependencyProperty RadProperty =
DependencyProperty.Register(
"Radius",
typeof(double),
typeof(uc));
}
and the XAML:
<UserControl x:Class="WpfApplication18.uc"
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>
<StackPanel>
<TextBox Text="{Binding Path=DataSource[0]}"></TextBox>
<TextBox Text="{Binding Path=DataSource[1]}"></TextBox>
<TextBox Text="{Binding Path=Radius}"></TextBox>
<TextBox Text="{Binding Path=Radius}"></TextBox>
</StackPanel>
</Grid>
</UserControl>
精彩评论