首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >禁用WPF DataGrid上的行编辑

禁用WPF DataGrid上的行编辑
EN

Stack Overflow用户
提问于 2015-12-08 14:10:40
回答 2查看 2.4K关注 0票数 2

我这里需要帮助。我试图在WPF DataGrid上禁用一些行编辑。这是我的XAML:

代码语言:javascript
复制
<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>

这是我的ViewModel:

代码语言:javascript
复制
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");                
        }
    }
}

我的模型有一个属性:

代码语言:javascript
复制
public partial class CombustiblesWCFModel
{
    public Boolean IsReadOnly { get; set; }
}

所以,第1行应该是禁用的编辑或不是?

我的Finaly是手动启动行编辑(每行都有一个链接),并使用每一行上的另一个链接确认行编辑,以便使用WCF服务保存数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-08 14:19:28

您可以手动定义列。

代码语言:javascript
复制
<DataGridTemplateColumn>
   <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
           <TextBox Text="{Binding Path=YourField}" 
                    IsReadOnly="{Binding IsReadonly}"/>
       </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

希望这能有所帮助

票数 3
EN

Stack Overflow用户

发布于 2015-12-08 18:21:06

你的xaml很适合我。我会查看你的IsReadonly属性。检查Visual输出中的绑定表达式路径错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34158048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档