转换器的绑定错误

时间:2013-05-24 12:16:49

标签: c# mvvm binding converter

我在我的viewModel中有动态创建的MdiContainer:

    public MainWindowViewModel()
    {
         MdiContainer = new TabbedMdiContainer();
    }                                          
    public TabbedMdiContainer MdiContainer { get; private set; }

在Xaml中,我将此容器的内容设置为mdihost:

<docking:TabbedMdiHost x:Uid="ALTabbedMdiHost" Grid.Row="1" IsEnabled="True" Content="{Binding MdiContainer}" IsCloseButtonOnTab="False"/>

当我使用背景图像动态创建新标签时,我希望该图像填充mdiContainer的高度和宽度。为此,我制作了图像高度和宽度的装订。

 var height = new Binding("ActualHeight") { Source = MdiContainer, IsAsync = true};
 var width = new Binding("ActualWidth") { Source = MdiContainer ,IsAsync = true};
 _img.SetBinding(FrameworkElement.HeightProperty, height);
 _img.SetBinding(FrameworkElement.WidthProperty, width);

问题是高度包括MdiContainer高度标题高度,我需要MdiContainer的高度没有标题高度

所以我在不同的类中创建了转换器:

public class ImageSizeConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((double) value - 1000);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

但是当我试图将转换器添加到绑定值时:

 var height = new Binding("ActualHeight") { Source = MdiContainer, IsAsync = true, Converter = ImageSizeConvertor};

我收到错误此时类名无效。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我发现错误) var height = new Binding("ActualHeight") { Source = MdiContainer, IsAsync = true, Converter = new ImageSizeConvertor() };