删除WPF中的重复代码
在XAML代码中删除重复的代码时遇到困难。
示例:
<TabControl x:Name="TabControl">
<TabItem Header="1">
<TabItem.Content>
<GridView x:Name="GridView1">
...
</GridView>
</TabItem.Content>
</TabItem>
<TabItem Header="2">
<TabItem.Content>
<GridView x:Name="GridView2">
...
</GridView>
</TabItem.Content>
</TabItem>
</TabControl>
选项卡的内容是相同的,除了GridView控件的名称。
我需要通过XAML静态创建选项卡,以便访问Code-Behind的GridView。
有没有一种方法可以将TabiTem的内容保存在一个地方,因此可以用于两个TabiTem,但可以保留在代码范围内分别访问GridView的可能性?
Having trouble removing duplicated code in XAML code.
Example:
<TabControl x:Name="TabControl">
<TabItem Header="1">
<TabItem.Content>
<GridView x:Name="GridView1">
...
</GridView>
</TabItem.Content>
</TabItem>
<TabItem Header="2">
<TabItem.Content>
<GridView x:Name="GridView2">
...
</GridView>
</TabItem.Content>
</TabItem>
</TabControl>
Contents of tabs are identical, except for the names of the GridView controls.
I need tabs to be statically created through XAML, in order to access the GridView's in code-behind.
Is there a way to keep the content of TabItems in one place, so it could be used for both TabItems, but to preserve the possibility to access both GridView's separately in code-behind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您指的是整个GridView内容,则可以像形式一样创建自己的控件。但是,您不仅
可以创建一个带有GridView内容的文件
,例如在您的选项卡控件中,您可以添加此自定义XAML内容
“ local”的上下文“ local”,就像您在现有自己的项目和上一步中创建的类名称。我已经为许多可以在多个地方重复使用的事情做了这种方式。想象一个需要名称和地址的应用程序。为什么要重新创建20次。声明网格,列对齐,文本输入等相同的上下文,并在该级别和内部零件(提供相同的绑定属性名称上下文)中拥有任何绑定上下文,并且可以顺利进行。 hth
反馈
如果您更完整[GridView]并且每个实例都有多个控件,并且您想明确地知道该控件,则您仍然可以使用X:名称的零件上下文,但是您首先需要获得pertions gridview名称,然后找到其中其他控件的部分。因为gridview1.sometextbox在其陈述中与gridview1.sometextbox明显不同。有意义吗?如果您可以编辑现有帖子,并且可能会提供更多有关您要做的事情的上下文,则可能有助于增加澄清。
另一种方法可能是将自己的自定义类带有某些属性,并具有绑定到预定义的模板/样式的上下文,该模板/样式知道您的自定义类及其相关的可绑定属性,但这比您的位置要多得多。目前。
If you are referring to the entire GridView content, you can create your own control just as you would a form. But instead of
Just create a file with the content of your gridview such as
Then, in your tab control that is duplicated, you could add this custom xaml content
The context of "local" as you would see in your existing is basically context to your own project and the class name created in the previous step. I have done this way for many things that can get re-used in multiple places. Imagine an app that needs name and addresses. Why recreate 20 times. Declare the same context of grid, column alignments, text entry, etc and just have whatever binding context assigned at that one level and the inner pieces (provided same binding property names context) and it works smoothly. HTH
Feedback
If your more complete [GridView] and each instance has multiple controls and you want to know the controls within that explicitly, you can still use the x:Name context of the parts, but you would first need to get the first instance gridview name, then FIND the parts of the other controls within it. Because GridView1.SomeTextbox would be an obviously different object than GridView1.SomeTextbox in ITs presentation. Make sense? If you can EDIT your existing post and maybe provide a little more context of what you are trying to do MIGHT help add clarification.
Another way might be to make your own custom class with certain properties and have the context of those bound to a pre-defined TEMPLATE/STYLE that knows of your custom class and its related Bindable Properties, but that gets into a bit more than where you are at the moment.