簡體   English   中英

為什么不綁定到“畫布的寬度和高度”屬性?

[英]Why do not bind to the properties Width and Height of Canvas?

為什么不綁定到“畫布的寬度和高度”屬性?

我試圖那樣做,但是沒有。

  <ItemsControl
                Grid.Row="0"
                ItemsSource="{Binding RectItems}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                                //does not work
                        <Canvas Height="{Binding PanelHeight, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                Width="{Binding PanelWidth, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                    </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>
                        <Rectangle
                            Width="{Binding Width}"
                            Height="{Binding Height}"
                            Fill="Black" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

這些綁定似乎很好用。 只需添加

Background="Red"

畫布的定義,以便您可以查看實際的畫布。

我給你舉了一個例子:

班級

 public class RectItem : INotifyPropertyChanged
    {
        private int _width;
        private int _height;
        private int _x;
        private int _y;

        public Brush Color { get; set; }

        public int Width {
            get { return _width; }
            set {
                if (value == _width) return;
                _width = value;
                OnPropertyChanged();
            }
        }

        public int Height {
            get { return _height; }
            set {
                if (value == _height) return;
                _height = value;
                OnPropertyChanged();
            }
        }

        public int X {
            get { return _x; }
            set {
                if (value == _x) return;
                _x = value;
                OnPropertyChanged();
            }
        }

        public int Y {
            get { return _y; }
            set {
                if (value == _y) return;
                _y = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    internal class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            this.RectItems.Add(new RectItem { Height = 100, Width = 100, X = 0, Y = 0, Color = Brushes.DeepPink });
            this.RectItems.Add(new RectItem { Height = 50, Width = 50, X = 100, Y = 100, Color = Brushes.DeepSkyBlue });
        }

        private double _panelWidth = 100;
        private double _panelHeight = 100;
        public ObservableCollection<RectItem> RectItems { get; } = new ObservableCollection<RectItem>();


        public ICommand IncreaseSizeCommand => new RelayCommand(x =>
        {
            this.PanelHeight = 200;
            this.PanelWidth = 200;
        });

        public double PanelWidth {
            get { return _panelWidth; }
            set {
                if (value.Equals(_panelWidth)) return;
                _panelWidth = value;
                OnPropertyChanged();
            }
        }

        public double PanelHeight {
            get { return _panelHeight; }
            set {
                if (value.Equals(_panelHeight)) return;
                _panelHeight = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

XAML

<StackPanel>
            <Button Content="Click me" Width="80" Height="20" Command="{Binding IncreaseSizeCommand}"/>
            <ItemsControl
                Grid.Row="0"
                ItemsSource="{Binding RectItems}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Height="{Binding PanelHeight, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                Width="{Binding PanelWidth, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                    </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>
                        <Rectangle
                            Width="{Binding Width}"
                            Height="{Binding Height}"
                            Fill="{Binding Color}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
</StackPanel>

按方面工作。 我添加了一些顏色以進行視覺突出顯示,並添加了更改Canvas大小的命令

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM