C# ViewModel属性在';相应的CustomControl属性已更新
我正在尝试将VideoPreview自定义控件类中的IntPtr VidHandle属性绑定到其视图模型(vm:DebugHiresCamerawindViewModel)中的IntPtr PreviewHandle 在VideoPreview的构造函数中,我调用:C# ViewModel属性在';相应的CustomControl属性已更新,c#,wpf,mvvm,data-binding,custom-controls,C#,Wpf,Mvvm,Data Binding,Custom Controls,我正在尝试将VideoPreview自定义控件类中的IntPtr VidHandle属性绑定到其视图模型(vm:DebugHiresCamerawindViewModel)中的IntPtr PreviewHandle 在VideoPreview的构造函数中,我调用: this.VidHandle = picBox.Handle; 更新视频预览的VidHandleProperty DependencyProperty属性。这很好用。但是,未更新ViewModel中的PreviewHandle属性
this.VidHandle = picBox.Handle;
更新视频预览的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 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>
并加上
void VideoPreview_Loaded(object sender, RoutedEventArgs e)
{
this.VidHandle = picBox.Handle;
}
在视图中,更新绑定:
VidHandle="{Binding PreviewHandle, UpdateSourceTrigger=PropertyChanged}"
您可能还需要
Mode=OneWayToSource
,假设句柄应该只来自VideoPreview
,谢谢您的响应,但是当我进行这些更改时,当我单击按钮时,DebugHiResCameraWindowViewModel的PreviewHandle属性仍然为0,该按钮告诉我VideoPreview类中VidHandle的值没有影响到它在视图模型中的绑定属性。我的VideoPreview自定义控件类是否正确实现?我不需要打电话来更新视图模型?您的更改非常有效!关键是在已加载的事件处理程序中而不是在构造函数中设置VideoPreview类的VidHandle控件构造函数中的code>在绑定VidHandle=“{binding PreviewHandle…}
建立之前执行。因此绑定只会覆盖VidHandle
的值。您可以在VidHandle
的属性更改回调中设置picBox.Handle
。
void VideoPreview_Loaded(object sender, RoutedEventArgs e)
{
this.VidHandle = picBox.Handle;
}
VidHandle="{Binding PreviewHandle, UpdateSourceTrigger=PropertyChanged}"