在选择数据网格中的特定行后如何突出显示画布中的特定矩形

时间:2019-02-17 06:04:39

标签: c# wpf mvvm

我在WPF窗口(MVVM)中有一个canvas和datagrid,datagrid由4列(Width,Height,CanvasTop,CanvasLeft)组成,而datagrid在canavs部分中由3行和3个矩形组成。一旦我选择了datagrid row,则相应的特定矩形应突出显示。 例如:datagrid行的Width,Height,Canvas Top,Canvas Left分别具有50、50、100、100,则应突出显示宽度50,画布顶部100高度50,画布左侧100的矩形。

MainWindow.xaml 

<ItemsControl ItemsSource="{Binding RectangleData, Mode=TwoWay}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Background="LightYellow"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Canvas.Left" Value="{Binding Left, Mode=TwoWay}"/>
                        <Setter Property="Canvas.Top" Value="{Binding Top, Mode=TwoWay}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Width="{Binding Width, Mode=TwoWay}" Height="{Binding Height, Mode=TwoWay}" Style="{StaticResource StyleRectangle}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

ViewModel.cs

 public class MainViewModel:ViewModelBase
    {
        private ICommand _AddRectangleCmd;
        private ObservableCollection<Attributes> _RectangleData;
        private Attributes _SelectedRectangle;

        public RelayCommand MyCommand { get; private set; }

        public  MainViewModel()
        {          
            RectangleData = new ObservableCollection<Attributes>();
            MyCommand = new RelayCommand(Excute);
        }

        public ICommand AddRectangleCmd
        {
            get
            {
                if(_AddRectangleCmd == null)
                {
                    _AddRectangleCmd = new RelayCommand(param => 
                                       OpenRectangleForm(), null);                  
                }
                return _AddRectangleCmd;
            }
            set
            {
                _AddRectangleCmd = value;
                OnPropertyRaised("AddRectangleCmd");
            }
        }


        public ObservableCollection<Attributes> RectangleData
        {
            get
            {
                return _RectangleData;
            }
            set
            {
                _RectangleData = value;
                OnPropertyRaised("RectangleData");

            }
        }

        public Attributes SelectedRectangle
        {
            get
            {
                return _SelectedRectangle;
            }
            set
            {
                if (_SelectedRectangle != value)
                {
                    _SelectedRectangle = value;
                    OnPropertyRaised("SelectedRectangle");   
                }
            }
        }

        public void Excute(object attribute)
        {
            SelectedRectangle = (Attributes)attribute;
        }

        private void OpenRectangleForm()
        {
            AttributeWindow win = new AttributeWindow(this);
            win.Show();
        }

        public void DrawRectangle(Attributes att)
        {
            try
            {
                //To Draw Rectangle in the Canvas and Add to the DataGrid
                RectangleData.Add(att);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

选择任何数据网格行后,画布中的相应矩形将突出显示。

0 个答案:

没有答案