如何在DataGrid中允许单元格编辑

时间:2013-12-05 16:45:25

标签: c# wpf datagrid

我有一个从XML文件填充的dataGrid。 文件中的所有值都显示良好。 我添加了一些其他列到我的DataGrid的问题,我需要让用户 编辑这些列的内容。 当我尝试编辑内容时,没有任何内容出现,它总是空白。

这是我的DataGrid Xaml:

  <DataGrid 
        ItemsSource="{Binding Path=Elements[Parameter]}"
        AutoGenerateColumns="False" Height="Auto" 
        Name="DataGridParamScenarios" Grid.Row="1" MaxHeight ="250"
        Block.TextAlignment="Center" HorizontalAlignment =" Stretch"   HorizontalContentAlignment ="Stretch"
        Background="WhiteSmoke" RowBackground="LightYellow"
        AlternatingRowBackground="LightBlue" RowHeight="30" CanUserAddRows="False"
        SelectedItem="{Binding Path=SelectedParameter, Mode=TwoWay}"
        ScrollViewer.VerticalScrollBarVisibility="Visible" 
        CurrentCellChanged ="DataGridParamScenarios_CurrentCellChanged">

           <DataGrid.Columns>

              <DataGridTextColumn 
                Header="Name" IsReadOnly ="True"
                Binding="{Binding Path=Element[Name].Value}" Width ="*"/>
              <DataGridTextColumn 
                Header="Category" 
                Binding="{Binding Path=Element[Category].Value}" Width ="Auto" IsReadOnly="True"/>
              <DataGridTextColumn 
                Header="Value" 
                Binding="{Binding Path=Element[Value].Value}" Width ="*" IsReadOnly="False"/>
              <DataGridTextColumn 
                Header="Unit" 
                Binding="{Binding Path=Element[Unit].Value}" Width ="*" IsReadOnly="False" />
              <DataGridTextColumn 
                Header="Min" Width ="*" IsReadOnly="False"/>
              <DataGridTextColumn 
                Header="Max" Width ="*" IsReadOnly="False"/>

                </DataGrid.Columns>

        </DataGrid>

我需要编辑的列是Min和Max。 任何帮助PLZ。

修改

我将最后两列绑定到局部变量,但始终没有任何内容

 <DataGridTextColumn 
                Header="Min" 
                Binding="{Binding Path= MinValue}"
                Width ="*" IsReadOnly="False"/>
 <DataGridTextColumn 
                Header="Max" 
                Binding="{Binding Path= MaxValue}"
                Width ="*" IsReadOnly="False"/>

这是背后的代码:

 public partial class Scenarios : Window
{

    string MinValue = "0";
    string MaxValue = "20";
 public Scenarios()
    {
        InitializeComponent();
        var xml = XDocument.Load(XmlPath).Root;
        DataGridParamScenarios.DataContext = xml;
    }
}

1 个答案:

答案 0 :(得分:1)

WPF无法直接绑定到本地变量。您必须在属性中包含MinValueMaxValue

相关问题