wpf 如何在Datagrid中仅使某些行的某些列可编辑

k97glaaz  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(183)
<Grid>
    <igDP:XamDataGrid x:Name="U_XamDataGrid"
                      BorderBrush="{DynamicResource BorderColorBrush}"
                      DataSource="{Binding GetData.MSInform}"
                      GroupByAreaLocation="None"
                      GroupByAreaMode="MultipleFieldLayoutsFull"
                      InitializeRecord="U_XamDataGrid_InitializeRecord"
                      IsGroupByAreaExpanded="True"
                      ScrollViewer.VerticalScrollBarVisibility="Visible"
                      Theme="Office2013">
        <igDP:XamDataGrid.Resources>
            <SolidColorBrush x:Key="{ComponentResourceKey {x:Type igDP:XamDataGrid}, LabelBackground}" Color="{DynamicResource BackColorWhite}" />
        </igDP:XamDataGrid.Resources>

        <igDP:XamDataGrid.FieldSettings>
            <igDP:FieldSettings AllowEdit="false" LabelClickAction="Nothing" />
        </igDP:XamDataGrid.FieldSettings>

        <igDP:XamDataGrid.FieldLayoutSettings>
            <igDP:FieldLayoutSettings AllowClipboardOperations="All"
                                      AutoFitMode="Always"
                                      AutoGenerateFields="False"
                                      CopyFieldLabelsToClipboard="True"
                                      HeaderPlacementInGroupBy="OnTopOnly"
                                      HeaderPrefixAreaDisplayMode="FieldChooserButton"
                                      RecordSelectorLocation="None" />
        </igDP:XamDataGrid.FieldLayoutSettings>

        <igDP:XamDataGrid.FieldLayouts>
            <igDP:FieldLayout Key="MeasureSetInformationsSt">
                <igDP:FieldLayout.SortedFields>
                    <igDP:FieldSortDescription Direction="Ascending"
                                               FieldName="Title"
                                               IsGroupBy="True" />
                </igDP:FieldLayout.SortedFields>

                <igDP:FieldLayout.Fields>
                    <igDP:Field Name="Title"
                                Width="Auto"
                                AllowGroupBy="True"
                                Visibility="Collapsed" />

                    <igDP:Field Name="Name"
                                Width="Auto"
                                Label="{x:Static lang:Lang.Name}" />

                    <igDP:TemplateField Name="Value" Label="{x:Static lang:Lang.Value}">
                        <igDP:TemplateField.Settings>
                            <igDP:FieldSettings AllowEdit="{Binding Path=DataContext.IsEditable, ElementName=xamDataGrid}" />
                        </igDP:TemplateField.Settings>
                        <igDP:TemplateField.DisplayTemplate>
                            <DataTemplate>
                                <TextBlock Text="{igEditors:TemplateEditorValueBinding}" />
                            </DataTemplate>
                        </igDP:TemplateField.DisplayTemplate>
                        <igDP:TemplateField.EditTemplate>
                            <DataTemplate>
                                <igEditors:XamComboEditor IsAlwaysInEditMode="True" Value="1" />
                            </DataTemplate>
                        </igDP:TemplateField.EditTemplate>
                    </igDP:TemplateField>
                </igDP:FieldLayout.Fields>
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>-->
    </igDP:XamDataGrid>
</Grid>

字符串
我想只允许特定位置的行可编辑。

  • 视图模型
public bool IsEditable { get; set; } = false;
public string Title { get; set; }
public string Name { get; set; }
public string Value { get; set; }

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title = Lang.MeasurementCondition,
    Name  = Lang.Mode,
    Value = probe.Mode.ToString(),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.Project,
    Value    = GetData.ProjectARC.Name,
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title      = Lang.MeasurementCondition,
    Name       = Lang.Probe,
    IsEditable = true,
    Value      = probe.No.ToString(),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.SpectrometerResolution,
    Value    = CommonMethod.GetDescriptionFromEnum((SPCTROMETER_RESOLUTION)(int)probe.SingleModeMeasureSetting.Resolution),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.BackgroundGainLevel,
    Value    = CommonMethod.GetDescriptionFromEnum((GAIN_LEVEL)probe.SingleModeMeasureSetting.BackgroundGain),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.SampleGainLevel,
    Value    = CommonMethod.GetDescriptionFromEnum((GAIN_LEVEL)probe.SingleModeMeasureSetting.SampleGain),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.Averaging,
    Value    = probe.SingleModeMeasureSetting.Averaging.ToString(),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.BoxcarWidthEnable,
    Value    = probe.SingleModeMeasureSetting.BoxcarWidthEnable.ToString(),
});

GetData.MSInform.Add(new MeasureSetInformationsSt()
{
    Title    = Lang.MeasurementCondition,
    Name     = Lang.BoxcarWidth,
    Value    = probe.SingleModeMeasureSetting.BoxcarWidth.ToString(),
});


IsEditable = true在ComboBox表单中只有一部分行必须是可编辑的。我仍然不确定这样做是否正确。在输出窗口中发生绑定错误。我不知道如何正确绑定。错误详细信息已附加。
系统.Windows.数据错误:第二章:找不到目标元素的控制FrameworkElement或FrameworkContentElement。BindingExpression:Path=DataContext.IsEditable; DataItem=null;目标元素为“FieldSettings”(HashCode=45408440);目标属性为“AllowEdit”(类型为“Nullable”1“)

vngu2lb8

vngu2lb81#

<igDP:FieldSettings AllowEdit="{Binding Path=DataContext.IsEditable, ElementName=xamDataGrid}" />

字符串
有几个问题你需要先弄清楚:

  • “igDP”是你目前使用的一个UI库或一个nugget包吗?你是否在“XamDataGrid”上查看了他们的源代码或文档/示例(如果可用)
  • 你在这里发布的绑定错误基本上是说编译器没有找到你引用的UI元素。你确定“xamDataGrid”是你要绑定的控件吗?

最后,回到你的问题“只允许特定位置的行是可编辑的”,你的意思是你希望一些行在满足某些条件或要求时是可编辑的吗?如果是这样,那么很容易将AllowEdit绑定到ViewModel的属性,而不是视图,并在VM中编写逻辑。

相关问题