我在我的XAML中使用了10個如下所示的網格,每次都使用其他綁定源。 我必須重複(複製/粘貼)這段代碼10次還是有更好的方法? (模板?)如何防止重複這段代碼10次?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<TextBox
Style="{StaticResource TBGrid}"
Grid.Column="0"
Grid.Row="0"
Text="{Binding ...}" />
<Label
Style="{StaticResource TBLabel}"
Grid.Column="1"
Grid.Row="0"
Content="{Binding....}" />
</Grid>
現在我有這樣的代碼,從評論者的幫助下,它似乎工作:
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); }
}
}
}
XAML:
<UserControl
x:Class="MVVMCable.ArrowLabel"
x:Name="MyArrowLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="137.8"
d:DesignWidth="279.2">
<Grid
Width="70"
Height="15">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="40"/>
<ColumnDefinition
Width="30"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="14" />
</Grid.RowDefinitions>
<TextBox
Height="20"
Grid.Column="0"
Grid.Row="0"
IsEnabled="False"
HorizontalContentAlignment="Right"
Width="30"
Text="{Binding ElementName=MyArrowLabel, Path=TextBoxText}"/>
<Label
Height="auto"
Grid.Column="1"
Grid.Row="0"
HorizontalContentAlignment="Left"
Width="auto"
Content="{Binding ElementName=MyArrowLabel, Path=LabelText}"/>
</Grid>
</UserControl>
我用它在我的這樣的應用程序:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:MVVMCable"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:shapes="clr-namespace:MVVMCable.Views"
xmlns:core="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MVVMCable.MainWindow"
.......
<c:ArrowLabel
Canvas.Left="10"
Canvas.Top="6"
TextBoxText="{Binding Cable.RHVc}" <== error here and
LabelText="{Binding Units[X].Display}" /> <== here
錯誤文本是:
錯誤2成員「LabelText」未被識別或無法訪問。
並添加this.DataContext = this後,事情似乎工作。
它會幫助,如果你已經表明至少重複2次,並填補了數據綁定的點。你使用MVVM,一個框架? –