現在、WPFイメージコントロールをカプセル化するイメージビューアコントロールと、さらにフィルタを適用したりビューを変更するコントロールを記述しています。ここでは、コントロールのソースコードの関連部分です:カスタムタイプの依存関係プロパティを持つユーザーコントロール
public partial class ImageViewPort : UserControl, INotifyPropertyChanged
{
private BitmapSource _source;
public static readonly DependencyProperty ImageDescriptorSourceProperty =
DependencyProperty.Register("ImageDescriptorSource",
typeof(ImageDescriptor),
typeof(ImageViewPort),
new UIPropertyMetadata(ImageDescriptorSourceChanged));
public ImageDescriptor ImageDescriptorSource
{
get { return (ImageDescriptor)GetValue(ImageDescriptorSourceProperty); }
set { SetValue(ImageDescriptorSourceProperty, value); }
}
public BitmapSource Source //the image control binds to this beauty!
{
get { return _source; }
set { _source = value; OnPropertyChanged("Source"); }
}
public ImageViewPort() { InitializeComponent(); }
private static void ImageDescriptorSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ImageViewPort viewPort = (ImageViewPort)d;
if (viewPort != null)
{
viewPort.TransformImage();
}
}
private BitmapSource TransformImage()
{
//do something that sets the "Source" property to a BitmapSource
}
}
ユーザーコントロールのXAMLコード(関連部分のみ)は:
<UserControl x:Name="viewPort">
<Image Source="{Binding ElementName=viewPort,Path=Source}"/>
</UserControl>
そして最後に使用:私の中
<WPF:ImageViewPort ImageDescriptorSource="{Binding Path=CurrentImage}"/>
ウィンドウでは、基本的にCollectionを繰り返し、CurrentImageプロパティのPropertyChanged通知をスローします。それがうまくいくと、ゲッターが毎回呼び出されるので、バインディングが機能するようです。
私は今、UserControlのPropertyChanged-Callbackが起動されたが、そのようなことは起こりません(ブレークポイントを使って試したことはありません)。私は同じものをプリミティブ型(int)にバインドしようとしましたが、それはうまくいきました。
私の実装に欠陥がありますか?ユーザーコントロールが更新されないのはなぜですか? ご協力いただきありがとうございます!
乾杯
SEBI
出力を確認してください...バインディング警告はありますか?また、新しい価値を設定していますか? WPFは、既に設定されている値を設定しようとすると知り、無視します。メタデータ型をFrameworkPropertyMetadataに指定し、適切なデフォルト値を指定することをお勧めします。 – dowhilefor
この '{バインディングElementName =ビューポート、パス=ソース}' は、あなたの 'viewPort'要素に' Source' DPがあることを暗示していますが、これは実際に使用しているXAMlですか? –
@dowhilefor:どのように愚かな私のコンソールウィンドウでデータバインディングの例外について忘れて:)そのおかげで! –