C# 如何将元素绑定到选定的menuitem wpf

C# 如何将元素绑定到选定的menuitem wpf,c#,wpf,xaml,binding,menuitem,C#,Wpf,Xaml,Binding,Menuitem,我正在通过itemsSource属性向菜单项添加项。(元素名称为videoCapDevices) 现在我想对选定的菜单项做出响应,可能是用选定的摄像设备显示视频流 VideoCaptureDevice="{Binding Path=SelectedItem, ElementName=videoCapDevices}" 但这只适用于组合框或列表框 如何更改“路径选择器”以到达所选菜单项 谢谢尝试使用转换器进行转换 public class DeviceConverter : IValueConv

我正在通过itemsSource属性向菜单项添加项。(元素名称为videoCapDevices)

现在我想对选定的菜单项做出响应,可能是用选定的摄像设备显示视频流

VideoCaptureDevice="{Binding Path=SelectedItem, ElementName=videoCapDevices}"
但这只适用于组合框或列表框

如何更改“路径选择器”以到达所选菜单项


谢谢

尝试使用转换器进行转换

public class DeviceConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
       if(value==null)
          return null;

        return ((Device)value).CaptureDeviceName;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}


尝试使用转换器进行强制转换

public class DeviceConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
       if(value==null)
          return null;

        return ((Device)value).CaptureDeviceName;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}


设备只是一个静态源。。。例如ItemsSource=“{Binding Source={x:静态控件:MultimediaUtil.VideoInputDevices}}”以及VideoInputDevices如何运行,为MenuItem播放?我会在调试器中查看SelectedItem以了解发生了什么。Devices只是一个静态源。。。例如ItemsSource=“{Binding Source={x:Static Controls:MultimediaUtil.VideoInputDevices}}}”以及VideoInputDevices在MenuItem中如何运行?我会在调试器中查看SelectedItem以了解发生了什么。现在编译器抛出一个异常,它无法转换类型(字符串)转换成IValueConverter类型。。。事情是,在最初的例子中,从工作。。。与上面的方法完全相同,只是使用组合框而不是菜单项。。。我认为问题在于菜单项的路径选择。我更新了我的转换器making
CaptureDeviceName
属性,因为输出@onkelFungusi没有找到此属性,您在哪里找到它?你是说
DsDevice.Name
?我尝试使用返回值
DsDevice
,但编译器再次抛出相同的异常。。。你能告诉我你的意思吗@seddik laraba
XamlParseException
,但是在InnerException中有一个
InvalidCastException
(类型字符串不能转换为类型IValueConverter),所以我认为他从绑定中获得了一个String对象,但我不明白他为什么不能转换它……现在编译器抛出一个异常,它不能转换类型(字符串)在IValueConverter类型中…事情是,在最初的示例中,从worked…与上面完全相同,只是使用组合框而不是menuitem…我认为问题在于menuitem的路径选择。我更新了我的converter making
CaptureDeviceName
属性,因为输出@onkelFungusi在你找到了吗?你的意思是
DsDevice.Name
?我尝试使用返回值
DsDevice
,但编译器再次抛出相同的异常…你能告诉我你的意思吗?@seddik laraba
XamlParseException
,但在内部异常中有一个
invalidcasteption
(类型字符串无法转换为类型IValueConverter),所以我认为他从绑定中获得了一个字符串对象,但我不明白他为什么不能转换它。。。
 VideoCaptureDevice="{Binding Converter=DeviceConverter ,Path=SelectedItem, ElementName=videoCapDevices}"