标签: datagridcell

WPF DataGrid:如何确定当前行索引?

我正在尝试基于DataGrid实现一个非常简单的电子表格功能.

  1. 用户单击一个单元格

  2. 用户键入值并按回车键

  3. 扫描当前行,并更新依赖于单击的单元格的任何单元格公式.

这似乎是我的要求的最佳事件处理程序:

private void my_dataGrid_CurrentCellChanged(object sender, EventArgs e)
Run Code Online (Sandbox Code Playgroud)

问题:如何检测当前行的行索引?

c# wpf datagrid datagridcell

18
推荐指数
2
解决办法
4万
查看次数

如何制作WPF DataGridCell ReadOnly?

我知道你可以准备好整个DataGrid或整个列(IsReadOnly = true).但是,在单元级别,此属性仅准备就绪.但我确实需要这种级别的粒度.有一篇关于在DataGrid是公共领域时通过更改源代码来添加IsReadOnly的博客,但现在我没有DataGrid的源代码.什么是解决方法?

禁用单元格(IsEnabled = false)几乎满足了我的需要.但问题是你甚至无法点击禁用的单元格来选择行(我有完整的行选择模式).

编辑:由于没有人回答这个问题,所以我想这不是一个简单的解决方案.这是一个可能的解决方法:使单元格不可编辑.唯一的问题是单击单元格不会选择行.我刚刚注意到,当单击禁用的单元格时,仍会触发DataGrid的MouseDown或MouseUp事件.在这个事件处理程序中,如果我能找出它单击的行,我可以以编程方式选择行.但是,我无法弄清楚如何从中找到基础行DataGrid.InputHitTest.有人可以给我一些小费吗?

wpf readonly datagridcell

12
推荐指数
3
解决办法
3万
查看次数

WPF - 如何从DataGridRow获取单元格?

我有一个数据绑定的DataGrid与交替的行背景颜色.我想根据它包含的数据对单元格进行不同的着色.我已经尝试过这个线程建议的解决方案

http://wpf.codeplex.com/Thread/View.aspx?ThreadId=51143

但,

DataGridCellsPresenter presenter = GetVisualChild(row)

始终返回null.

我在用

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
Run Code Online (Sandbox Code Playgroud)

但DataGridRow的VisualTreeHelper.GetChildrenCount()始终返回0.我已经验证DataGridRow不为null并且已经填充了数据.任何帮助表示赞赏.

谢谢.

wpf datagrid datagridcell

10
推荐指数
2
解决办法
2万
查看次数

从DataGrid中选择DataGridCell

我有一个DataGridWPF控件,我想得到一个特定的DataGridCell.我知道行和列索引.我怎样才能做到这一点?

我需要,DataGridCell因为我必须能够访问其内容.因此,如果我有(例如)一列DataGridTextColum,我的内容将是一个TextBlock对象.

.net c# wpf wpfdatagrid datagridcell

10
推荐指数
1
解决办法
2万
查看次数

将DataGridCell ToolTip属性绑定到DataGridCell的值

我有DataGrid,其中一个DataGrid列看起来像这样

<DataGridTextColumn Header="Value" 
        Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" 
        x:Name="_col2" 
        IsReadOnly="True"
        CanUserSort="False"
        Width="*">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)

问题是我被迫使用BooleanToYesNoConverter转换器两次.这意味着Convert的方法BooleanToYesNoConverter将被调用两次.因此,我想优化我的代码.并希望将ToolTip属性值直接绑定到单元格的值.

我尝试使用ElementName-s方法.但我不知道我应该在绑定的Path属性中指定什么.

<DataGridTextColumn Header="Value" 
        Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" 
        x:Name="_col2" 
        IsReadOnly="True"
        CanUserSort="False"
        Width="*">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding ElementName=_col2, Path=???}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)

我尝试使用DataGridTemplateColumn而不是DataGridTextColumn,但它也不起作用.

<DataGridTemplateColumn CanUserSort="False"
                        Header="????????"
                        Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" …
Run Code Online (Sandbox Code Playgroud)

c# wpf tooltip datagridcell

8
推荐指数
1
解决办法
8439
查看次数

DataGrid选项卡导航跳过列

我在WPF中有一个带有模板列的数据网格.网格中的几列是只读的,焦点上的其他列变得可编辑(而不是标签,文本框,复选框等).

我想要实现的是当我浏览网格列时跳过readonly列.

谁知道如何实现这一目标?

谢谢!弗拉丹


不,不工作:(

这是完整的单元格...单独使用KeyboardNavigation.IsTabStop和IsTabStop尝试了...没有用

<DataGridTemplateColumn Header="{x:Static local:MainWindowResources.gasNameLabel}" Width="*" MinWidth="150" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding Path=Name}" ContentTemplate="{StaticResource DataGridTextBoxView}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsReadOnly" Value="true">
                    <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

wpf datagrid focus keyboard-navigation datagridcell

7
推荐指数
1
解决办法
5637
查看次数

具有验证错误样式的WPF Datagrid单元格

我正在尝试在出现验证错误时更改DataGridCell的默认样式(在WPF Toolkit DataGrid中).默认为红色边框.我怎样才能放置自己的模板?

谢谢.

validation wpf datagrid wpftoolkit datagridcell

5
推荐指数
2
解决办法
1万
查看次数

继承DataGridTextColumn并重写GenerateElement

我正在尝试创建一个自定义DataGrid,我可以根据单元格值格式化单个单元格(即;负值为红色文字,后置为绿色)ala这种方法...

如何在WPFToolkit DataGrid中获取当前单元格的绑定值

我还需要将值从负值转换为括号(即:-2.34到(2.34)).我有继承/过度工作.我的问题是,如何访问重写的GenerateElement方法中的单元格中的值.

菲利,提前谢谢

wpf datagrid datagridtextcolumn datagridcell

5
推荐指数
1
解决办法
1641
查看次数

WPF DataGrid绑定DataGridCell内容

希望这将是一个非常简单的答案,我只是没有看到我认为的树木的谚语.

我有一个DataGridCell样式,我想将单元格的内容绑定到图像的source属性,这是我目前使用的XAML:

<Style x:Key="DataGridImageCellStyle" TargetType="{x:Type toolkit:DataGridCell}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type toolkit:DataGridCell}">
                <Border Background="Transparent" 
              BorderBrush="{TemplateBinding BorderBrush}"  
              BorderThickness="0" 
              SnapsToDevicePixels="True">
                    <Image Source="{Binding RelativeSource={RelativeSource AncestorType=toolkit:DataGridCell}, Path=Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

请注意,目前我正在将图像源绑定到内容..这不起作用,我也尝试过Value,这不起作用!

所以我的问题是,很好,很简单..用于将单元格的内容放入此图像的source属性的正确绑定是什么?

提前致谢!

皮特

c# data-binding wpf datagrid datagridcell

4
推荐指数
1
解决办法
7346
查看次数

带有TextBlock的WPF DataGridCell模板 - 绑定?

我更换ContentPresenterDataGridCellTemplateTextBlock的,现在我搜索正确Binding的内容.

正常的方法是Text="{TemplateBinding Content}TextBlock-这是行不通的.也Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content, Mode=TwoWay}"行不通.

还有其他想法吗?

wpf replace textblock contentpresenter datagridcell

4
推荐指数
1
解决办法
8754
查看次数

WPF datagrid - 启用选择,禁用文本输入

我有一个C#WPF Datagrid,带有复选框列,超链接列和文本列.我的DataGrid绑定到DataTable.列不是自动生成的,但我确实在代码中动态创建它们,因为事先不知道列数.我想启用单元格中的文本(用于ctrl + c目的),但禁用编辑.我不希望文本被更改.有人可以帮忙吗?

wpf datagrid edit datagridcell

3
推荐指数
1
解决办法
9367
查看次数

从Silverlight 4中的代码隐藏更改DataGridCell的背景

我正在编写一个silverlight应用程序,它允许您通过输入的分隔符解析复制的文本.在解析数据并将其放入网格后,用户可以"清理"数据.这会将单元格的当前值与列的允许值进行比较,选择其最佳猜测并通过ItemsSource属性将数据重新绑定到网格.

我的问题是,我知道每个单元格的坐标已被"擦洗",我想突出显示此单元格或更改其背景颜色.据我所知,您可以单独设置DataGridCell的背景,但我无法访问DataGridCell.我可以访问Grid的列和行,但是这些似乎也没有像我希望的那样提供对DataGridCell的访问.在设置ItemsSource后,有没有人有办法访问DataGridCell?

silverlight datagrid datagridcell

1
推荐指数
1
解决办法
4130
查看次数