复合集合绑定

时间:2015-07-20 13:44:58

标签: wpf wpf-controls

我尝试使用复合集合(稍后将添加几个集合),但元素根本不会出现在屏幕上。怎么了? 将集合绑定到数据控件直接工作正常。 这是我的UserControl

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WPFTest.Views"
         xmlns:Model="clr-namespace:WPFTest.Model"
         xmlns:viewModels="clr-namespace:WPFTest.ViewModels"
         x:Class="WPFTest.Views.PolygonEditorView"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Slider  Value="{Binding ScaleFactor}" Minimum="-4" Maximum="5" Interval="1" Orientation="Vertical" IsSnapToTickEnabled="True" TickPlacement="Both"/>
    <ScrollViewer Width="1000" Height="1000" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Column="1">
        <ItemsControl Name="ItemsControl" >
            <ItemsControl.ItemsSource>
                <CompositeCollection >
                    <CollectionContainer Collection="{Binding Points}"/>
                </CompositeCollection>
            </ItemsControl.ItemsSource>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Width="{Binding CanvasSize}" Height="{Binding CanvasSize}" Background="RoyalBlue"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="viewModels:EditorPoint" >
                    <Thumb Width="11" Height="11">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Ellipse Width="11" Height="11" Fill="Gold"/>
                            </ControlTemplate>
                        </Thumb.Template>
                    </Thumb>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

和型号:

 internal class PolygonEditorViewModel : PropertyChangedBase
{
    public BindableCollection<EditorPoint> Points { get; set; }

    private int _scaleFactor;

    public int ScaleFactor
    {
        get { return _scaleFactor; }
        set
        {
            _scaleFactor = value;
            foreach (var editorPoint in Points)
            {
                editorPoint.ScaleFactor = value;
            }
            NotifyOfPropertyChange(() => CanvasSize);
            NotifyOfPropertyChange(() => ScaleFactor);
            Points.Refresh();
        }
    }


    public double CanvasSize => 10000*Math.Pow(2, ScaleFactor);

    public PolygonEditorViewModel()
    {
        Points = new BindableCollection<EditorPoint>()
        {
            new EditorPoint() {Vertex = new Vertex() {X = 3500, Y = 3000}},
            new EditorPoint() {Vertex = new Vertex() {X = 6500, Y = 3000}},
            new EditorPoint() {Vertex = new Vertex() {X = 3500, Y = 7000}},
            new EditorPoint() {Vertex = new Vertex() {X = 6500, Y = 7000}},
            new EditorPoint() {Vertex = new Vertex() {X = 2500, Y = 5000}},
            new EditorPoint() {Vertex = new Vertex() {X = 7500, Y = 5000}}
        };
        ScaleFactor = -4;
    }
}

0 个答案:

没有答案