如何在C#/ wpf中启用datagrid列文本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在C#/ wpf中启用datagrid列文本相关的知识,希望对你有一定的参考价值。

我有一个应用程序,只要我点击“插入”我希望程序启用数据网格中的文本,用户可以输入产品的信息。有人可以帮助我,我该怎么做?

在应用程序代码和图片下方

enter image description here

<Window>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" FontWeight="Bold" HorizontalAlignment="Center">Produtos</TextBlock>
        <TextBlock Grid.Row="0" Grid.Column="1" FontWeight="Bold" HorizontalAlignment="Center">Unidades</TextBlock>
        <TextBlock Grid.Row="0" Grid.Column="2" FontWeight="Bold" HorizontalAlignment="Center">Empresas</TextBlock>
        <TextBlock Grid.Row="0" Grid.Column="3" FontWeight="Bold" HorizontalAlignment="Center">Operadores</TextBlock>
        <DataGrid Grid.Row="1" Grid.Column="0" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaProdutos}" AutoGenerateColumns="False" SelectedItem="{Binding ProdutoSelecionado}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Descrição" Binding="{Binding Descricao, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid Grid.Row="1" Grid.Column="1" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaUnidades}" AutoGenerateColumns="False" SelectedItem="{Binding UnidadeSelecionada}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Descrição" Binding="{Binding Descricao, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid Grid.Row="1" Grid.Column="2" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaEmpresas}" AutoGenerateColumns="False" SelectedItem="{Binding EmpresaSelecionada}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Descrição" Binding="{Binding Descricao, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid Grid.Row="1" Grid.Column="3" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaOperadores}" AutoGenerateColumns="False" SelectedItem="{Binding OperadorSelecionado}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Nome" Binding="{Binding Nome, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Command="{Binding InserirProdutoCommand}" Width="50" Height="25">Inserir</Button>
            <Button Command="{Binding ExcluirProdutoCommand}" Width="50" Height="25">Excluir</Button>
        </StackPanel>
        <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Command="{Binding InserirUnidadeCommand}" Width="50" Height="25">Inserir</Button>
            <Button Command="{Binding ExcluirUnidadeCommand}" Width="50" Height="25">Excluir</Button>
        </StackPanel>
        <StackPanel Grid.Row="2" Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Command="{Binding InserirEmpresaCommand}" Width="50" Height="25">Inserir</Button>
            <Button Command="{Binding ExcluirEmpresaCommand}" Width="50" Height="25">Excluir</Button>
        </StackPanel>
        <StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Command="{Binding InserirOperadorCommand}" Width="50" Height="25" HorizontalAlignment="Center">Inserir</Button>
            <Button Command="{Binding ExcluirOperadorCommand}" Width="50" Height="25" HorizontalAlignment="Center">Excluir</Button>
        </StackPanel>
        <Border Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" BorderThickness="0 1 0 0" BorderBrush="Gray" Margin="10,10,5,0" Padding="5 5 5 0" VerticalAlignment="Top" Height="46">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Command="{Binding SalvarCommand}" Width="80" Height="30">Salvar</Button>
                <Button Command="{Binding CancelarCommand}" Width="80" Height="30">Cancelar</Button>
            </StackPanel>
        </Border>
    </Grid>
</Window>
答案

我找到了满足require的解决方案,但它需要绑定到Datagrid的对象有一个布尔属性。这是xaml:

<DataGridTemplateColumn Width="120">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox
                                Name="innerTextBox"
                                BorderThickness="0"
                                IsReadOnly="{Binding DisableEdit, UpdateSourceTrigger=PropertyChanged}"
                                Text="{Binding Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

这里是对象:public class Person:BindableBase {private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private bool disableEdit;

    public bool DisableEdit
    {
        get { return disableEdit; }
        set { SetProperty(ref disableEdit, value); }
    }

}

这是按钮点击事件:

        var person = datagrid.SelectedCells[0].Item as Person;

        person.DisableEdit = false;
另一答案

UpdateSourceTrigger=PropertyChanged放入DataGrid绑定而不是DataGridTextColumn

以上是关于如何在C#/ wpf中启用datagrid列文本的主要内容,如果未能解决你的问题,请参考以下文章

找不到将Wpf DataGrid列绑定到属性的方法[关闭]

Wpf DataGrid插入列,列的类型问题

合并标题列datagrid wpf

如何在 WPF 的 Datagrid 中设置以编程方式生成的 ComboBox 的启用属性?

如何在 WPF Datagrid 上启用滚动条?

如何在所有列标题上启用文本换行?