简体   繁体   中英

Disable row edit on WPF DataGrid

I need help here. I'm trying to disable some rows edit on a WPF DataGrid. This is my XAML:

<DataGrid ItemsSource="{Binding Path=Combustibles}" AutoGenerateColumns="False" SelectedItem="{Binding Path=SelectedCombustible}">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsReadOnly}" Value="True" >
                    <Setter Property="IsEnabled" Value="False" />                                
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
</DataGrid>

And this is my ViewModel:

public class CombustiblesViewModel: BaseViewModel
{        
    public List<CombustiblesWCFModel> Combustibles{ get; set; }

    private CombustiblesWCFModel _selectedcombustible = new CombustiblesWCFModel();

    public CombustiblesViewModel()
    {
        Combustibles = _svc.Combustibles_List(sTicket);
        Combustibles[1].IsReadOnly = true;
    }            

    public CombustiblesWCFModel SelectedCombustible
    {
        get
        {
            return this._selectedcombustible;
        }

        set
        {
            this._selectedcombustible = value;
            NotifyOfPropertyChange("SelectedCombustible");                
        }
    }
}

My Model has a Property:

public partial class CombustiblesWCFModel
{
    public Boolean IsReadOnly { get; set; }
}

So, Row 1 should be disable for edditing or isn't?

My Finaly boal is to manually start row edit (With a link on each row) and confirm row edit with another link on each row to save data using a WCF services.

You could define your columns manually

<DataGridTemplateColumn>
   <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
           <TextBox Text="{Binding Path=YourField}" 
                    IsReadOnly="{Binding IsReadonly}"/>
       </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Hope this helps

Your xaml works for me fine. I would look into your IsReadonly property. Check for binding expression path errors in your Visual Studio output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM