English 中文(简体)
阅读数据
原标题:Make first row of DataGrid ReadOnly
  • 时间:2012-01-12 09:56:21
  •  标签:
  • wpf
  • datagrid

I m利用数据Grid在我的WPF应用中展示用户许可。

数据组的第一行将始终包含展示许可的项目的所有人。

在项目创建时,该所有人即被设定,不能直接从数据单上改变。

我的问题是。

我怎么能够读一读,也许会给它一个具体风格,以便改变背景?

最佳回答

你们将需要一个触发因素,唯一的问题是,在码头数据网上设置一个排位指数是相当的,因此,我倾向于这样做:

    <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsOwner}" Value="true">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

如果标的上具有财产价值,则该行将具有独特性。 因此,如果存在独一无二的身份证,或者所有权人总是拥有的,那么你就可以对此负责。

The setter property is IsEnabled as datagridrow doesn t contain a property for readonly, but this will stop a user modifying the row.

问题回答

在此,我要谈谈这个问题。 先前的样本将仅对你进行细微,我的样本是证明对囚室进行低水平控制的方法。 在WPFDataGridRow上,你只能使用所附的特性,例如<代码>Enabled、FontW 8,等,因为这些物质传播到电池级,但实际控制线在电池一级界定。

只读写的24小时一般比残疾的茶箱更清洁,你还可能想对你囚室的阅读和阅读模式采用完全不同的风格,因为你不得不这样做与以下的守则相类似。

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReadOnlyRows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (o, e) =>
            {
                this.g.ItemsSource = new List<Person>(2)
                {
                    new Person(){ Name="Dmitry", Role="Owner" },
                    new Person(){ Name="Jody", Role="BA" }
                };
            };
        }
    }

    public class Person
    {
        public string Role
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }
    }

    public class PersonServices
    {
        // that shouldn t be in template selector, whould it?
        public static bool CanEdit(Person person)
        {
            return person.Role != "Owner";
        }
    }

    public class TemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Person person = item as Person;

            if (person == null) return null;

            string templateName = PersonServices.CanEdit(person) ? "EditableDataTemplate" : "ReadOnlyDataTemplate";

            return (DataTemplate)((FrameworkElement)container).FindResource(templateName);
        }
    }

    public class EditingTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Person person = item as Person;

            if (person == null) return null;

            string templateName = PersonServices.CanEdit(person) ? "EditableEditingDataTemplate" : "ReadOnlyEditingDataTemplate";

            return (DataTemplate)((FrameworkElement)container).FindResource(templateName);
        }
    }
}

XAML:

<Window x:Class="ReadOnlyRows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ReadOnlyRows"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="g" AutoGenerateColumns="False"
                  CanUserAddRows="False">
            <DataGrid.Resources>
                <DataTemplate x:Key="EditableEditingDataTemplate">
                    <TextBox Text="{Binding Name}" />
                </DataTemplate>

                <DataTemplate x:Key="ReadOnlyEditingDataTemplate">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                </DataTemplate>

                <DataTemplate x:Key="EditableDataTemplate">
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>

                <DataTemplate x:Key="ReadOnlyDataTemplate">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                </DataTemplate>

                <local:TemplateSelector x:Key="TemplateSelector" />
                <local:EditingTemplateSelector x:Key="EditingTemplateSelector" />
            </DataGrid.Resources>

            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name"
                    CellTemplateSelector="{StaticResource TemplateSelector}"
                    CellEditingTemplateSelector="{StaticResource EditingTemplateSelector}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签