WPF Canvas,如何使用后面的MVVM代码动态添加子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5913792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:50:39  来源:igfitidea点击:

WPF Canvas, how to add children dynamically with MVVM code behind

wpfmvvmcanvasdynamicchildren

提问by Sathya Ram

Requirement:

要求:

To draw one Bitmap Image and rectangle(s) based on the collection of points. The rectangle should exactly fit on the pixels location over the image. There is also some text need to be added inside the rectangle.

根据点的集合绘制一个位图图像和矩形。矩形应该完全适合图像上的像素位置。矩形内还需要添加一些文本。

The Image will be always only one and the rectangles will be dynamically added.

图像将始终只有一个,矩形将被动态添加。

Current Solution:

当前解决方案:

Have a canvas with Image Control. Add the the dynamic code under the code behind file ViewImageResult.xaml.cs.

有一个带有图像控制的画布。在文件 ViewImageResult.xaml.cs 后面的代码下添加动态代码。

    private void DrawResult(int left, int right, int width, int height)
    {
        Border bord = new Border();
        bord.BorderThickness = new Thickness(1);
        bord.BorderBrush = Brushes.Red;
        bord.Width = width;
        bord.Height = height;
        _mainCanvas.Children.Add(bord);
        Canvas.SetLeft(bord, left);
        Canvas.SetTop(bord, right);
    }

Issue:

问题:

Since i follow MVVM pattern, the collection of points for rectangle is in my ViewModel file ViewImageResultModel.cs. I am not able to add the child rectangle dynamically from the ViewModel file.

由于我遵循 MVVM 模式,矩形的点集合在我的 ViewModel 文件 ViewImageResultModel.cs 中。我无法从 ViewModel 文件中动态添加子矩形。

回答by Kent Boogaart

ItemsControlis your friend:

ItemsControl是你的朋友:

<Grid>
    <Image Source="..."/>
    <ItemsControl ItemsSource="{Binding Points}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Red" BorderThickness="1" Width="{Binding Width}" Height="{Binding Height}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

The above assumes your VM exposes a collection of points via a Pointsproperty, and that each point VM has X, Y, Width, and Heightproperties.

以上假设你的VM通过暴露点的集合Points属性,每个点虚拟机有XYWidth,和Height属性。

回答by Sathya Ram

Added IsItemsHost="True"to the Canvasof Kent's solution:

添加IsItemsHost="True"CanvasKent 的解决方案中:

<Grid>
    <Image Source="..."/>
    <ItemsControl ItemsSource="{Binding Points}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas  IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Red" BorderThickness="1" Width="{Binding Width}" Height="{Binding Height}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>