以xaml绑定矩形的宽度和高度

Dal*_*ons 5 c# wpf xaml rect viewport

我想的宽度和高度结合Rect在一个ViewPort这样的:

<VisualBrush.Viewport>
    <Rect Width="{Binding Path=MyWidth}" Height="{Binding Path=MyHeight}"/>
</VisualBrush.Viewport>
Run Code Online (Sandbox Code Playgroud)

我的绑定在其他地方工作正常,但是在这里我收到以下错误消息:

不能在“ Rect”类型的“ Width”属性上设置“ Binding”。只能在DependencyObject的DependencyProperty上设置“绑定”。

编辑我了解错误消息。我的问题是如何解决它。如何绑定矩形的高度和宽度?

Cle*_*ens 5

使用这样的MultiBinding:

<VisualBrush.Viewport>
    <MultiBinding>
        <MultiBinding.Converter>
            <local:RectConverter/>
        </MultiBinding.Converter>
        <Binding Path="MyWidth"/>
        <Binding Path="MyHeight"/>
    </MultiBinding>
</VisualBrush.Viewport>
Run Code Online (Sandbox Code Playgroud)

使用这样的多值转换器:

public class RectConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return new Rect(0d, 0d, (double)values[0], (double)values[1]);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Run Code Online (Sandbox Code Playgroud)