0

カスタマイズの「radius」(double) と「contentSource」(string[]) のいくつかのパラメーターが必要な UserControl があります。

私の UserControl は、いくつかのネストされたコントロールで構成されています。

<UserControl ...>
<Grid>
    <my:Menu ...>
        <my:Button>
        </my:Button>
        <my:Button>
        </my:Button>
        <my:Button>
        </my:Button>
    </my:Menu ...>
</Grid>

私はパラメータを公開しようとしています:

    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));

ただし、「string[]」パラメーターがクラッシュを引き起こしているようですが、ほとんどの場合、「Radius」プロパティをまったく設定できません。パラメータを公開するために他に何かする必要がありますか?

4

1 に答える 1

2

どのように値にアクセスしようとしていますか? コードを UserControl にコピーしましたが、正常に動作しているようです。これらの値にアクセスしているオブジェクトの DataContext を設定しましたか?

これが役立つかもしれない私のテストコードです:

 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));

}

および 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>
于 2010-11-15T01:56:30.587 に答える