2014-03-29 81 views
0

我創建了我的UserControl,並在後面的代碼中添加了屬性CompanyName,FirstName和SecondName。我爲每個屬性添加了DependencyProperty。在xaml我使用LongListSelector,我想在ItemTemplate中綁定一些來自viewmodel的事物:observable集合(FirstName和SecondName)和一個字符串(CompanyName)。將datacontext綁定到usecontrol

由於某些原因,公司名稱的DependencyProperty未被觸發且該值未綁定。對於ObservableCollection,一切都是綁定的。

注意:我不想綁定TextBlock的Text屬性。這只是一個新項目,我正試圖解決這個問題。在真實的項目中,我使用代碼背後的這些屬性來處理事件。

我會給我的一半王國和我的女兒誰解決這個問題,因爲我面臨這個問題幾天。

Here is the example solution

這裏是代碼..

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> 

回答

1

我認爲你的樣品溶液,檢查並motify你的代碼,最後我達到你想要的,我發現了幾個問題。

  • MainPage頁面的代碼CompanyName="{Binding ElementName=self, Path=DataContext.CompanyName}",您的公司名稱綁定到self(頁),路徑DataContext的,但在背後LayoutRoot.DataContext = App.ViewModel;的代碼,所以更改LayoutRootthis

  • 更改財產CompanyName在類MainViewModel

  • 公共財產的方法CompanyNameChanged在CLAS添加sender.txtCompanyName.Text = newValue s MyUserControl。因爲控件初始化時,DataContext可能未初始化,所以當您設置txtCompanyName.Text = CompanyName時,CompanyName爲空。當CompanyName屬性發生更改時,您必須重置txtCompanyName

代碼:

sender.txtCompanyName.Text = newValue; 

願望這可以幫助你。如果您需要幫助,請將其標記爲答案。