我創建了我的UserControl,並在後面的代碼中添加了屬性CompanyName,FirstName和SecondName。我爲每個屬性添加了DependencyProperty。在xaml我使用LongListSelector,我想在ItemTemplate中綁定一些來自viewmodel的事物:observable集合(FirstName和SecondName)和一個字符串(CompanyName)。將datacontext綁定到usecontrol
由於某些原因,公司名稱的DependencyProperty未被觸發且該值未綁定。對於ObservableCollection,一切都是綁定的。
注意:我不想綁定TextBlock的Text屬性。這只是一個新項目,我正試圖解決這個問題。在真實的項目中,我使用代碼背後的這些屬性來處理事件。
我會給我的一半王國和我的女兒誰解決這個問題,因爲我面臨這個問題幾天。
這裏是代碼..
MyUserControl.cs
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
this.Loaded += MyUserControl_Loaded;
}
private void MyUserControl_Loaded(object sender, RoutedEventArgs e)
{
txtCompanyName.Text = CompanyName;
txtFirstName.Text = FirstName;
txtSecondName.Text = SecondName;
}
public string CompanyName
{
get
{
return (string)GetValue(CompanyNameProperty);
}
set
{
SetValue(CompanyNameProperty, value);
}
}
public static DependencyProperty CompanyNameProperty = DependencyProperty.Register("CompanyName", typeof(string), typeof(MyUserControl), new PropertyMetadata("", CompanyNameChanged));
private static void CompanyNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as MyUserControl;
if (sender == null) return;
var newValue = e.NewValue as string;
sender.CompanyName = newValue;
}
public string FirstName
{
get
{
return (string)GetValue(FirstNameProperty);
}
set
{
SetValue(FirstNameProperty, value);
}
}
public static DependencyProperty FirstNameProperty = DependencyProperty.Register("FirstName", typeof(string), typeof(MyUserControl), new PropertyMetadata("", FirstNameChanged));
private static void FirstNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as MyUserControl;
if (sender == null) return;
var newValue = e.NewValue as string;
sender.FirstName = newValue;
}
public string SecondName
{
get
{
return (string)GetValue(SecondNameProperty);
}
set
{
SetValue(SecondNameProperty, value);
}
}
public static DependencyProperty SecondNameProperty = DependencyProperty.Register("SecondName", typeof(string), typeof(MyUserControl), new PropertyMetadata("", SecondNameChanged));
private static void SecondNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as MyUserControl;
if (sender == null) return;
var newValue = e.NewValue as string;
sender.SecondName = newValue;
}
}
代碼隱藏MainPage.xaml中
<phone:PhoneApplicationPage
x:Class="PhoneApp6.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:sharecontrols="clr-namespace:PhoneApp6"
x:Name="self">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector
x:Name="MainLongListSelector"
Margin="0,0,-12,0"
ItemsSource="{Binding Employees}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<sharecontrols:MyUserControl
FirstName="{Binding FirstName}"
SecondName="{Binding SecondName}"
CompanyName="{Binding ElementName=self, Path=DataContext.CompanyName}"
/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</Grid>
</phone:PhoneApplicationPage>