Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止这段代码重复10次?_C#_Wpf_Xaml - Fatal编程技术网

C# 如何防止这段代码重复10次?

C# 如何防止这段代码重复10次?,c#,wpf,xaml,C#,Wpf,Xaml,在我的XAML中,我使用了下面的10个网格,每次使用其他绑定源。 我是否必须重复(复制/粘贴)此代码10次,还是有更好的方法?(模板?) 现在,在评论员的帮助下,我有了这段代码,它似乎起到了作用: using System.Windows; using System.Windows.Controls; namespace MVVMCable { /// <summary> /// Interaction logic for ArrowLabel.xaml

在我的XAML中,我使用了下面的10个网格,每次使用其他绑定源。 我是否必须重复(复制/粘贴)此代码10次,还是有更好的方法?(模板?)


现在,在评论员的帮助下,我有了这段代码,它似乎起到了作用:

using System.Windows;
using System.Windows.Controls;

namespace MVVMCable
{
    /// <summary>
    /// Interaction logic for ArrowLabel.xaml
    /// </summary>
    public partial class ArrowLabel : UserControl
    {
        public ArrowLabel()
        {
            InitializeComponent();
            this.DataContext = this;  //  <==== this must be added, seemingly.
        }
        public static readonly DependencyProperty TextBoxTextProperty = DependencyProperty.Register
            (
                "TextBoxText",
                typeof(string),
                typeof(ArrowLabel),
                new PropertyMetadata("")
            );

        public string TextBoxText
        {
            get { return this.GetValue(TextBoxTextProperty) as string; }
            set { this.SetValue(TextBoxTextProperty, value); }
        }

        public static readonly DependencyProperty LabelTextProperty = DependencyProperty.Register
            (
                "LabelText",
                typeof(string),
                typeof(ArrowLabel),
                new PropertyMetadata("")
            );

        public string LabelText
        {
            get { return this.GetValue(LabelTextProperty) as string; }
            set { this.SetValue(LabelTextProperty, value); }
        }
    }
}
使用System.Windows;
使用System.Windows.Controls;
名称空间MVVMCable
{
/// 
///ArrowLabel.xaml的交互逻辑
/// 
公共部分类ArrowLabel:UserControl
{
公共箭头标签()
{
初始化组件();

this.DataContext=this;//如果绑定在大多数情况下都可以标准化,并且您只能切换上下文,那么您应该创建一个
UserControl
,将您的内容放在那里。并在需要的任何地方使用它,而不是重复代码,只需更改绑定到的数据上下文即可。

如果绑定可以标准化d到大多数情况下,您只能切换上下文,然后您应该创建一个
用户控件
,将您的内容放在其中。并在需要的任何地方使用它,而不是重复代码,只需更改它们绑定到的数据上下文。

您可以通过依赖属性创建自定义字段控件。在此字段控件中,您可以组合标签nd文本框功能组合在一起。然后您可以将控件放在stackpanel中。这样,它将删除冗余代码。

您可以通过依赖属性创建自定义字段控件。在此字段控件中,您可以将标签和文本框功能组合在一起。然后您可以将控件放在stackpanel中。因此,它将删除冗余代码。

因为e每种类型都有不同的
DataContext
,我建议使用自定义
UserControl

例如:

UserControl
本身将具有重复的XAML

<UserControl
    x:Class="SpecialUserControl"
    x:Name="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid>                               
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
         </Grid.RowDefinitions>
         <TextBox
               Style="{StaticResource TBGrid}"
               Grid.Column="0"
               Grid.Row="0"
               Text="{Binding ElementName=MyUserControl, Path=TextBoxText}" />
         <Label
              Style="{StaticResource TBLabel}"
              Grid.Column="1"
              Grid.Row="0"
              Content="{Binding ElementName=MyUserControl, Path=LabelText}" />
    </Grid>
</UserControl>
如果您注意到,
UserControl
XAML实际上绑定到
TextBox
标签
控件的这些
dependencProperties

现在使用时,您只需使用定义的
dependencProperties
绑定到您想要的任何内容:

<namespace:SpecialUserControl 
    TextBoxText="{Binding ThisIsABinding}" 
    LabelText="{Binding ThisIsAnotherBinding}"/>

因为每个控件都有不同的
DataContext
,所以我建议使用自定义的
UserControl

例如:

UserControl
本身将具有重复的XAML

<UserControl
    x:Class="SpecialUserControl"
    x:Name="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid>                               
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
         </Grid.RowDefinitions>
         <TextBox
               Style="{StaticResource TBGrid}"
               Grid.Column="0"
               Grid.Row="0"
               Text="{Binding ElementName=MyUserControl, Path=TextBoxText}" />
         <Label
              Style="{StaticResource TBLabel}"
              Grid.Column="1"
              Grid.Row="0"
              Content="{Binding ElementName=MyUserControl, Path=LabelText}" />
    </Grid>
</UserControl>
如果您注意到,
UserControl
XAML实际上绑定到
TextBox
标签
控件的这些
dependencProperties

现在使用时,您只需使用定义的
dependencProperties
绑定到您想要的任何内容:

<namespace:SpecialUserControl 
    TextBoxText="{Binding ThisIsABinding}" 
    LabelText="{Binding ThisIsAnotherBinding}"/>


如果您至少显示了2次重复,并为数据绑定填充了点,这会有所帮助。您是否使用MVVM,一个框架?如果您至少显示了2次重复,并为数据绑定填充了点,这会有所帮助。您是否使用MVVM,一个框架?非常感谢您为我指明了正确的方向。我将研究这个问题@Erik没问题!如果您有任何问题,请告诉我。嗯……我在XAML中发现一个错误:“LabelText”成员未被识别或无法访问。(TextBoxText也是如此)。所有其余的编译都正常:UserControl XAML及其CB,只有我的应用程序的XAML显示错误消息。@Erik我刚刚修复了它。依赖项属性的名称中实际上缺少一个“P”。请参阅更新。确保它显示的是“属性”而不是“属性”我发现了一个,或者更确切地说是VS2013被它绊倒了,但它并没有使错误消失。奇怪的是,我没有在上得到一个错误,非常感谢你为我指出了正确的方向。我会研究这个。@Erik没问题!如果你有任何疑问,请告诉我。嗯……我在XAML中得到了一个错误:成员“LabelText”无法识别或无法访问。(与TextBoxText相同)。所有其余部分编译正常:UserControl XAML及其CB,只有我的应用程序的XAML显示错误消息。@Erik我刚刚修复了它。依赖项属性的名称中实际上缺少一个“P”。请参阅更新。确保它显示的是“属性”而不是“属性”我发现了一个,或者更确切地说是VS2013被它绊倒了,但它并没有使错误消失。奇怪的是,我没有在
public static readonly DependencyProperty TextBoxTextProperty = 
    DependencyProperty.Register
    (
        "TextBoxText", 
        typeof(string), 
        typeof(SpecialUserControl),
        new PropertyMetadata("")
    );

public string TextBoxText
{
    get { return this.GetValue(TextBoxTextProperty) as string; }
    set { this.SetValue(TextBoxTextProperty, value); }
}

public static readonly DependencyProperty LabelTextProperty = 
    DependencyProperty.Register
    (
        "LabelText", 
        typeof(string), 
        typeof(SpecialUserControl),
        new PropertyMetadata("")
    );

public string LabelText
{
    get { return this.GetValue(LabelTextProperty) as string; }
    set { this.SetValue(LabelTextProperty, value); }
}
<namespace:SpecialUserControl 
    TextBoxText="{Binding ThisIsABinding}" 
    LabelText="{Binding ThisIsAnotherBinding}"/>