WPF 编辑状态切换

    有时候DataGrid编辑的时候一个属性需要根据别的属性呈现不同的编辑状态。这就需要一个做一个状态切换。比如地址是1的时候,读写类型是读写、只读、只写。地址是2的时候,就只读。状态栏切换为TextBook不能修改。效果如下:

    WPF 编辑状态切换

   WPF 编辑状态切换

  1.先需要在资源字典中定义2个模板:

<UserControl.Resources>

        <ResourceDictionary>

        <DataTemplate x:Key="TextBlockTemplate" DataType="viewModel:IoDataItemViewModel" >

                <TextBlock Text="{Binding ReadWriteTypeStr,UpdateSourceTrigger=PropertyChanged}"  TextAlignment="Center"  VerticalAlignment="Stretch"   />

            </DataTemplate>

            <DataTemplate x:Key="ComBoxTemplate" DataType="viewModel:IoDataItemViewModel">

                <ComboBox Text="{Binding ReadWriteTypeStr, UpdateSourceTrigger=PropertyChanged}" Loaded="FrameworkElement_OnLoaded"

                          ItemsSource="{x:Static viewModel:ReadWriteTypeView.ReadWriteTypes}" SelectedValuePath="Key"  DisplayMemberPath="Value" 

                          BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">

                </ComboBox>

            </DataTemplate>

        </ResourceDictionary>

    </UserControl.Resources>

2.定义模板选择器

   定义了一个TextBlock和Combox模板。在xaml中初始化。

  public class IoItemReadWriteTypeGridCellTemplateSelector : DataTemplateSelector

    {

        public DataTemplate ComBoxTemplate { get; set; }

        public DataTemplate TextBlockTemplate { get; set; }



        public override DataTemplate SelectTemplate(object item, DependencyObject container)

        {

            var ioitem = item as IoDataItemViewModel;

            if (ioitem == null)

                return base.SelectTemplate(item, container);



            var type = IOAddressHelper.GetReadWriteType(ioitem.IoDeviceViewModel.IoDevice.DriverName, ioitem.Address);



            if (type != ReadWriteType.ReadWrite)

            {

                return TextBlockTemplate;

            }

            return ComBoxTemplate;

        }

    }

3.在Grid Column定义中绑定对象。初始化TextBlockTemplate和 ComBoxTemplate, 这样在触发编辑状态的时候,会根据地址的不同出现不同的编辑状态

   <syncfusion:GridTemplateColumn  HeaderText="读写类型" MappingName="ReadWriteType" MinimumWidth="100" ColumnSizer="SizeToHeader" HorizontalHeaderContentAlignment="Center">

                    <syncfusion:GridTemplateColumn.CellTemplate>

                        <DataTemplate DataType="viewModel:IoDataItemViewModel">

                            <TextBlock Text="{Binding ReadWriteTypeStr,UpdateSourceTrigger=LostFocus}" HorizontalAlignment="Center" VerticalAlignment="Center" />

                        </DataTemplate>

                    </syncfusion:GridTemplateColumn.CellTemplate>

                    <syncfusion:GridTemplateColumn.EditTemplate>

                        <DataTemplate DataType="viewModel:IoDataItemViewModel">

                            <TextBlock Text="{Binding ReadWriteTypeStr,UpdateSourceTrigger=LostFocus}"   />

                        </DataTemplate>

                    </syncfusion:GridTemplateColumn.EditTemplate>

                    <syncfusion:GridTemplateColumn.EditTemplateSelector>

                        <local:IoItemReadWriteTypeGridCellTemplateSelector  TextBlockTemplate="{StaticResource TextBlockTemplate}" ComBoxTemplate="{StaticResource ComBoxTemplate}"></local:IoItemReadWriteTypeGridCellTemplateSelector>

                    </syncfusion:GridTemplateColumn.EditTemplateSelector>

                </syncfusion:GridTemplateColumn>

 

 

    

你可能感兴趣的:(WPF)