c# - 创建一个自定义模板作为 ResourceDictionary 并从代码隐藏中访问它

标签 c# xaml windows-store-apps datatemplate resourcedictionary

我需要创建一个简单的 CRUD,并且我想在不同的页面中重用相同的"template"。

MyCrud.xaml(模板)

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!-- disegno la mia crud-->
<DataTemplate x:Key="MyCrud">
    <StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,20,0,20">
            <TextBlock Text="Code"  FontWeight="Bold" Width="150" FontSize="24"/>
            <TextBox x:Name="edtCode" InputScope="Number" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,20,0,20">
            <TextBlock Text="Vin" FontWeight="Bold" Width="150" FontSize="24"/>
            <TextBox x:Name="edtVin" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,20,0,20">
            <TextBlock Text="Url" FontWeight="Bold" Width="150" FontSize="24"/>
            <TextBox x:Name="edtUrl" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/>
        </StackPanel>
    </StackPanel>
</DataTemplate>

然后我在我的 App.xaml 中注册了它

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View/MyCrud.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

所以现在我想将其放入页面中,并在我的 SearchPage.xaml 中使用它

SearchPage.xaml

<Page ...bla bla bla>
<ContentControl x:Name="MyTemplate" Content="{Binding}" ContentTemplate="{StaticResource MyCrud}" />
</Page>

目前布局工作正常

问题是:

  1. 如何访问文本框?例如那个叫 edtVin 来自 SearchPage 的代码隐藏。
  2. 为什么 TextBox p= GetTemplateChild("edtVin") as TextBox; 始终为 null?
  3. 我找到了这个solution ,这是最好的吗?

谢谢!

最佳答案

"Create a proper ViewModel and use proper DataBinding and all your problems will magically disappear."

DataTemplate 旨在用作“特定数据元素的视觉表示”

无需“访问”WPF 过程代码中的任何 UI 元素。对这些元素的任何属性的任何修改都应该使用正确的 DataBinding 来完成,否则,如果你手动修改UI元素的属性(而不修改相应的底层Data Item),基本上就破坏了UI和Data之间的一致性,DataTemplate内部的UI元素将不再反射(reflect)数据项内的数据。

创建一个简单的类来表示您想要在屏幕上显示的数据:

public class MyData
{
    public string Code {get;set;}

    public string Vin {get;set;}

    public string Url {get;set;}
}

然后,为了支持双向 WPF DataBinding,请使用您的类 Implement the INotifyPropertyChanged interface .

然后,设置 UI 的 DataContext这些项目的相关实例(或集合):

public MyWindow() //Window's constructor
{
   InitializeComponent();

   var list = new List<MyData>();

   //... populate the list with data here...

   DataContext = list;
}

然后简单地操作这些数据项的属性,同时保留 UI:

var item = list[0]; //for example

item.Code = "My New Code";

这是使用 WPF 的正确方法。

关于c# - 创建一个自定义模板作为 ResourceDictionary 并从代码隐藏中访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204679/

相关文章:

c# - 如何从静态资源为 UWP 中的依赖属性添加默认值

c# - AvalonDock - 防止可锚定 Pane 停靠在文档 Pane 中

javascript - 通过 JavaScript 设置按钮文本

c# - 单元测试 - 基于算法还是基于样本?

c# - ComboBox - 从两个属性中选择对象

c# - 计算 VS_KEY 容器名称

windows-store-apps - 在 Windows 8.1 中生成应用程序包有什么作用?

c# - 将 Windows 应用商店应用程序连接到本地主机

c# - 向多个用户显示由另一个用户插入的数据更新

c# - MVC 使用 RenderPartial 访问类 List