ViewModel属性在';的相应CustomControl属性已更新
本文关键字:属性 已更新 CustomControl ViewModel | 更新日期: 2023-09-27 18:24:12
我正在尝试将VideoPreview自定义控件类中的IntPtr VidHandle属性绑定到其视图模型(vm:DugHiResCameraWindowViewModel)中的IntPtr PreviewHandle。
在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>
视频预览类:
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);
}
});
}
}
}
看起来VideoPreview
控件的VidHandle
绑定到了VideoPreview
控件(不存在)的PreviewHandle
。
当您开始调试时,请检查输出窗口,它应该向您显示绑定错误(例如,当它找不到属性时)。
也许这就是你想要的?
<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中设置它,它将被子控件所知。
回复:评论。。。VM通常优先——如果在所有getter/setter上设置断点,您可以看到顺序。。。0来自DP的默认值(这可以在meta中设置,但不能是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