更新相应的CustomControl属性时,ViewModel属性不会更新

时间:2014-09-24 18:45:32

标签: c# wpf mvvm data-binding custom-controls

我正在尝试将VideoPreview自定义控件类中的IntPtr VidHandle属性绑定到其视图模型中的IntPtr PreviewHandle(vm:DebugHiResCameraWindowViewModel)。

在VideoPreview的构造函数中,我调用:

this.VidHandle = picBox.Handle;

更新VideoPreview的VidHandleProperty DependencyProperty。这非常有效。但是,ViewModel中的PreviewHandle属性未更新。到我打电话的时候:

camera.StartVideoStream(PreviewHandle);

在视图模型中,PreviewHandle为0,因为它从未从VideoPreview更新。我感觉我的DependencyProperty VidHandleProperty没有正确实现,但我可能是错的。

以下是一些代码段:

主窗口XAML:

<Window 
x:Class="AoiImageLift.Views.DebugHiResCameraWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:AoiImageLift.Presentation.ViewModels"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:com="clr-namespace:AoiImageLift.Components"
xmlns:local="clr-namespace:AoiImageLift"
Title="DebugHiResCameraWindow"
Name="hiResWindow"
Height="300" 
Width="300">
<Window.Resources>
    <vm:DebugHiResCameraWindowViewModel x:Key="viewModel"/>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <com:VideoPreview
        DataContext="{StaticResource viewModel}"
        x:Name="videoHost"
        VidHandle="{Binding PreviewHandle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type com:VideoPreview}}}"/>

    <Button
        DataContext="{StaticResource viewModel}"
        Grid.Row="1"
        Width="100"
        Height="40"
        Command="{Binding StartCaptureCommand}"
        Content="Start"/>
</Grid>

VideoPreview类:

public class VideoPreview : WindowsFormsHost
{
    private PictureBox picBox;
    public static readonly DependencyProperty VidHandleProperty =
        DependencyProperty.Register(
            "VidHandle",
            typeof(IntPtr),
            typeof(VideoPreview),
            new FrameworkPropertyMetadata
            {
                BindsTwoWayByDefault = true
            });

    public VideoPreview() : base()
    {
        picBox = new PictureBox();
        picBox.Width = (int) this.Width;
        picBox.Height = (int) this.Height;

        this.VidHandle = picBox.Handle;
        this.Child = picBox;
    }

    public IntPtr VidHandle
    {
        get 
        { 
            return (IntPtr) GetValue(VideoPreview.VidHandleProperty);
        }
        set 
        { 
            SetValue(VideoPreview.VidHandleProperty, value);
        }
    }
}

查看模型类:

public class DebugHiResCameraWindowViewModel : ViewModel
{
    private Uri capturedImage;
    private BitmapImage bmp;
    private ISnapImages camera;

    public DebugHiResCameraWindowViewModel() 
    {
        camera = LumeneraCamera.Instance;
        bmp = new BitmapImage();
    }

    public IntPtr PreviewHandle { get; set; }
    public Uri CapturedImage
    {
        get { return capturedImage; }
        set { capturedImage = value; OnPropertyChanged("CapturedImage"); }
    }
    public ICommand StartCaptureCommand
    {
        get
        {
            return new DelegateCommand(() =>
            {
                try
                {
                    camera.StartVideoStream(PreviewHandle);
                }
                catch (CustomException ex)
                {
                    MessageBox.Show(ex.Message, ex.Caption, ex.Button, ex.Image);
                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:1)

VidHandle控件的VideoPreview似乎与PreviewHandle控件的VideoPreview绑定(不存在)。

开始调试时检查输出窗口,它应该显示绑定错误(例如,当它找不到属性时)。

也许这就是你追求的目标?

<Window 
x:Class="AoiImageLift.Views.DebugHiResCameraWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:AoiImageLift.Presentation.ViewModels"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:com="clr-namespace:AoiImageLift.Components"
xmlns:local="clr-namespace:AoiImageLift"
Title="DebugHiResCameraWindow"
Name="hiResWindow"
Height="300" 
Width="300">
<!-- CHANGED HERE: set DataContext of Window -->
<Window.DataContext>
    <vm:DebugHiResCameraWindowViewModel />
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <!-- CHANGED HERE: removed DataContext (set at Window) -- updated binding -->
    <com:VideoPreview
        x:Name="videoHost"
        VidHandle="{Binding PreviewHandle}"/>

    <!-- CHANGED HERE: removed DataContext (set at Window) -- binding OK -->
    <Button
        Grid.Row="1"
        Width="100"
        Height="40"
        Command="{Binding StartCaptureCommand}"
        Content="Start"/>
</Grid>

DataContext是继承的,您可以在Window中设置它,它将由子控件知道。

Re:评论...... VM通常优先 - 如果你在所有getter / setter上设置断点,你可以看到顺序... 0来自DP的默认值(可以设置在元中,但它不能是PictureBox,因为DP是静态的)。不确定这是否合适,但它确实有效:

VideoPreview构造函数中,添加:

base.Loaded += VideoPreview_Loaded;

并添加

void VideoPreview_Loaded(object sender, RoutedEventArgs e)
{
    this.VidHandle = picBox.Handle;
}

在视图中,更新绑定:

VidHandle="{Binding PreviewHandle, UpdateSourceTrigger=PropertyChanged}"

您可能还需要Mode=OneWayToSource,假设句柄应仅来自VideoPreview