在代码隐藏中编辑 DataGridTemplateColumn?

发布于 2024-10-31 00:11:36 字数 789 浏览 1 评论 0原文

我认为这非常简单,但我到处搜索,似乎找不到答案。我有一个 DataGridTemplateColumn,我想用它来显示 DataGridDataContext 中不存在的值。即我有一个实体,根据文化有不同的名称。当网格加载时,我想根据当前的文化获取适当的名称。每次我看到有关 DataGridTemplateColumn 的任何内容时,它们总是使用 Binding 语法。我在这里不能这样做。我需要什么 C# 代码来访问以下 XAML 中的“nameValue”TextBlock,以及我应该在什么事件处理程序中访问它:

<Datagrid:DataGridTemplateColumn Header="Name" x:Name="nameField">
    <Datagrid:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock x:Name="nameValue" />
            </StackPanel>
        </DataTemplate>
    </Datagrid:DataGridTemplateColumn.CellTemplate>
</Datagrid:DataGridTemplateColumn>

提前感谢所有人,对于超级 n00b 问题我深表歉意。

I would think that this would be incredibly simple, but I've searched all over and can't seem to find an answer. I have a DataGridTemplateColumn that I want to use to display a value that isn't in the DataContext of the DataGrid. I.e. I have an entity that has different names based on the culture. When the grid loads, I want to go get the appropriate name based on the current culture. Every time I see anything about DataGridTemplateColumns, they're always using the Binding syntax. I can't do that here. What C# code do I need to access the "nameValue" TextBlock in the following XAML, and in what event handler should I access it:

<Datagrid:DataGridTemplateColumn Header="Name" x:Name="nameField">
    <Datagrid:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock x:Name="nameValue" />
            </StackPanel>
        </DataTemplate>
    </Datagrid:DataGridTemplateColumn.CellTemplate>
</Datagrid:DataGridTemplateColumn>

Thanks to all in advance and I'm sorry for the super n00b question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-11-07 00:11:36

您仍然可以使用绑定语法,听起来您只需要绑定到静态方法而不是网格的数据上下文。这里有一个很好的参考http:// /blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html 以此为例并根据您的情况进行修改。

第一:像平常一样设置网格、项目源和列、标准数据绑定。这将处理您需要从数据库或其他来源获取的任何列。

第二:在您的项目中添加静态类

namespace StaticBinding
{
        public class MyStaticClass
        {   
            private static string myStaticProperty;
            public static string MyStaticProperty
                {
                    get
                    {    return 
                            (CultureInfo.CurrentCulture.Name == "en-US" ? "US" : "Other");
                    }
                set { myStaticProperty = value; } }
        }
} 

第三:将新资源添加到应用资源中

 <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.SampleDataSource" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
         x:Class="SilverlightApplication28.App"
         xmlns:myns="clr-namespace:StaticBinding"
         >
<Application.Resources>
    <myns:MyStaticClass x:Name="MyStaticClass"></myns:MyStaticClass>
</Application.Resources>

最后:TextBlock中设置绑定,如果您已经构建了项目,您应该能够在绑定编辑器窗口中看到该属性。

   <sdk:DataGrid AutoGenerateColumns="False" Height="171" HorizontalAlignment="Left" Margin="61,53,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="263" ItemsSource="{Binding Collection}" LoadingRow="dataGrid1_LoadingRow"   Loaded="dataGrid1_Loaded"     >
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Binding="{Binding Property1}" Header="Property1"/>
            <sdk:DataGridCheckBoxColumn Binding="{Binding Property2}" Header="Property2"/>
            <sdk:DataGridTextColumn Binding="{Binding Property3}" Header="Property3"/>
            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock x:Name="nameValue" Text="{Binding Source={StaticResource MyStaticClass}, Path=MyStaticProperty}" />
                        </StackPanel>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

You can still use the binding syntax, it sounds like you just need to bind to a static method instead of the data context of the grid. There is a good reference here http://blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html Taking that as an example and modifying for your case.

First: Setup your grid as you would normally, item source and columns, standard data binding. This will take care of any columns you need from the database or other source.

Second: In your project add your static class

namespace StaticBinding
{
        public class MyStaticClass
        {   
            private static string myStaticProperty;
            public static string MyStaticProperty
                {
                    get
                    {    return 
                            (CultureInfo.CurrentCulture.Name == "en-US" ? "US" : "Other");
                    }
                set { myStaticProperty = value; } }
        }
} 

Third: Add your new resource to app resources

 <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.SampleDataSource" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
         x:Class="SilverlightApplication28.App"
         xmlns:myns="clr-namespace:StaticBinding"
         >
<Application.Resources>
    <myns:MyStaticClass x:Name="MyStaticClass"></myns:MyStaticClass>
</Application.Resources>

Finally: Set the binding in your TextBlock, if you've built your project, you should be able to see the property in the binding editor window.

   <sdk:DataGrid AutoGenerateColumns="False" Height="171" HorizontalAlignment="Left" Margin="61,53,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="263" ItemsSource="{Binding Collection}" LoadingRow="dataGrid1_LoadingRow"   Loaded="dataGrid1_Loaded"     >
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Binding="{Binding Property1}" Header="Property1"/>
            <sdk:DataGridCheckBoxColumn Binding="{Binding Property2}" Header="Property2"/>
            <sdk:DataGridTextColumn Binding="{Binding Property3}" Header="Property3"/>
            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock x:Name="nameValue" Text="{Binding Source={StaticResource MyStaticClass}, Path=MyStaticProperty}" />
                        </StackPanel>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文