3

新しく作成したユーザー コントロールのカスタム スタイルを設定しようとしていますが、「属性 'Property' の値を 'System.Windows.DependencyProperty' 型のオブジェクトに変換できません。」

私は Dependency プロパティを設定したと思っていましたが、そうではないようだったので、いくつかの調査を行って追加しました:

   public static readonly DependencyProperty ImageSourceProperty = 
   DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image));

-- MyButton.Xaml.Cs --

    namespace Client.Usercontrols
{
    public partial class MyButton : UserControl
    {
        public static readonly DependencyProperty ImageSourceProperty = 
            DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image));

        public MyButton()
        {
            InitializeComponent();            
        }


        public event RoutedEventHandler Click;

        void onButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.Click != null)
                this.Click(this, e);
        }

        BitmapSource _imageSource;
        public BitmapSource ImageSource
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                tehImage.Source = _imageSource;
            }
        }
    }
}

残念ながら、これは機能しません。私もこれを試しました:

public BitmapSource ImageSource
{
    get { return (BitmapSource)GetValue(MyButton.ImageSourceProperty); }
    set
    {
        SetValue(ImageSourceProperty, value);
    }
}

しかし、それは機能せず、画像は表示されず、前述と同じエラーが生成されました。

何か案は?コハンさんはじめまして。

-- MyButton.Xaml --

<UserControl x:Class="Client.Usercontrols.MyButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">


    <Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick">

        <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
            <Grid>
                <Image Name="tehImage" Source="{Binding ImageSource}" />
                <TextBlock Name="tehText" Text="{Binding Text}" Style="{DynamicResource ButtonText}" />
            </Grid>
        </Border>

    </Button>
</UserControl>

-- MYButton スタイル --

<Style TargetType="{x:Type my:MyButton}" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type my:MyButton}">
                <ContentPresenter />
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="ImageSource" Value="../Images/Disabled.png" />                        
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

4 に答える 4

4

Image私が見る最大の問題は、所有者ではなく所有者としてプロパティを登録していることですUserControl。への変更:

public static readonly DependencyProperty ImageSourceProperty = 
            DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(MyButton));

それでもうまくいかない場合は、XAML を確認する必要があります。

于 2009-10-20T10:00:39.930 に答える
3

依存関係プロパティの標準形式は次のとおりです (情報に追加しました)。

public BitmapSource ImageSource
{
    get { return (BitmapSource)GetValue(ImageSourceProperty); }
    set { SetValue(ImageSourceProperty, value); }
}

/* Using a DependencyProperty as the backing store for ImageSource. 
   This enables animation, styling, binding, etc... */

public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", 
            typeof(BitmapSource), 
            typeof(MyButton), 
            new UIPropertyMetadata(null)
        );

また、依存関係プロパティを「tehImage」というオブジェクトのImageSourceに渡そうとしているようです。これは、PropertyChangedCallback を使用して自動的に更新するように設定できます。これは、プロパティが更新されるたびに、更新が自動的に呼び出されることを意味します。

したがって、プロパティ コードは次のようになります。

public BitmapSource ImageSource
{
    get { return (BitmapSource)GetValue(ImageSourceProperty); }
    set { SetValue(ImageSourceProperty, value); }
}

/* Using a DependencyProperty as the backing store for ImageSource.  
   This enables animation, styling, binding, etc... */

public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", 
            typeof(BitmapSource), typeof(MyButton), 
            new UIPropertyMetadata(null, 
                ImageSource_PropertyChanged
            )
        );

private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    ((MyButton)source).tehImage.ImageSource = (ImageSource)e.NewValue
}

正しく登録された依存関係プロパティがあれば、問題を絞り込む (または修正する) のに役立つことを願っています。

于 2009-10-21T03:15:49.387 に答える
1

UserControl の DataContext を設定します。

public MyButton()
{
    InitializeComponent();
    DataContext = this;
}

または、それができない場合 (たとえば、DataContext が別のオブジェクトに設定されているため)、XAML でこれを行うことができます。

<UserControl x:Class="Client.Usercontrols.MyButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    x:Name="MyControl">


    <Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick">

        <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
            <Grid>
                <Image Name="tehImage" Source="{Binding ElementName=MyControl, Path=ImageSource}" />
                <TextBlock Name="tehText" Text="{Binding ElementName=MyControl, Path=Text}" Style="{DynamicResource ButtonText}" />
            </Grid>
        </Border>

    </Button>
</UserControl>
于 2009-10-21T03:01:59.507 に答える
0

私の意見では、ユーザー コントロールにイメージのソースを実装する正しい方法は BitmapSource ではありません。最も簡単で最良の方法 (私によると) は、Uri を使用することです。

依存関係プロパティを次のように変更します (コールバック イベントの変更も定義します)。

ImageSourceProperty = DependencyProperty.Register(
    "ImageSource", typeof (Uri), typeof (MyButton),
    new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));

そしてこれへのプロパティ:

public Uri ImageSource
{
    get
    {
           return (Uri)GetValue(ImageSourceProperty);
    }
    set
    {
           SetValue(ImageSourceProperty, value);
    }
}

コールバックは次のようになります。

private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    MyButton hsb = (MyButton)sender;

    Image image = hsb.tehImage;
    image.Source = new BitmapImage((Uri) e.NewValue);
}
于 2009-10-26T10:58:16.877 に答える