2017-09-07 28 views
0

我在ContentControlContentTemplate中使用了ComboBox。最初,我将DataContextProfession1)与内容属性ContentControl绑定在后面的代码中。ComboBox DataContex未更新放置在ContentControl(UWP)中的属性?

然后我在运行时更改ComboBox的值。之后,在运行时将ContentControlDataContext更改为Profession2。在这种情况下,ComboBoxDataContext未更改,前面的DataContextSelectedItem值设置为空。

我认为这是MSControl中的一个问题。请任何建议你的想法来解决这个问题。

请参考下面的代码:

查看:

<Page.Resources> 
     <local:MainPageViewModel x:Key="datacontent"></local:MainPageViewModel> 
    </Page.Resources> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 
      Margin="10"> 
     <ContentControl x:Name="contentControl" > 
      <ContentControl.ContentTemplate> 
       <DataTemplate> 
        <ComboBox x:Name="comboBox" 
           ItemsSource="{Binding Professions}" 
           SelectedItem="{Binding Profession, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"          
           HorizontalAlignment="Stretch"></ComboBox> 
       </DataTemplate> 
      </ContentControl.ContentTemplate> 
     </ContentControl> 
     <Button Click="Button_Click_2" Content="Change DataContext" Width="100" Height="50"></Button> 
    </Grid> 
</Page> 

型号&视图模型:

public class MainPageViewModel 
    { 
     public MainPageViewModel() 
     {    
      Profession1 = new Person(); 
      Profession2 = new Person();      
      private Person profession1; 

     public Person Profession1 
     { 
      get { return profession1; } 
      set { this.profession1 = value; } 
     } 

     private Person profession2; 

     public Person Profession2 
     { 
      get { return profession2; } 
      set { this.profession2 = value; } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

    } 

    public class Person : INotifyPropertyChanged 
    { 
     public Person() 
     { 
      _professions = new List<string>(); 
      _professions.Add("Lawyer"); 
      _professions.Add("Politician"); 
      _professions.Add("Other");   
     } 

     private string _profession; 
     public string Profession 
     { 
      get 
      { 
       if (string.IsNullOrWhiteSpace(_profession)) 
       { 
        // _profession = _professions.LastOrDefault(); 
       } 
       return _profession; 
      } 
      set 
      { 
       if (_profession != value) 
       { 
        _profession = value; 
        NotifyPropertyChanged("Profession"); 
       } 
      } 
     }  

     private List<string> _professions; 

     public List<string> Professions 
     { 
      get 
      { 
       return _professions; 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

代码背后:

public sealed partial class MainPage : Page 
    { 
     Binding binding; 
     public MainPage() 
     { 
      this.InitializeComponent(); 
      var datacontent = (this.Resources["datacontent"] as MainPageViewModel); 
      this.UpdateBinding1(this.contentControl, datacontent); 
     }   

     public void UpdateBinding1(FrameworkElement contentcontrol, object datacontext) 
     { 
      binding = new Binding(); 
      binding.Path = new PropertyPath("Profession1"); 
      binding.Mode = BindingMode.TwoWay; 
      binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
      binding.Source = datacontext; // view model 
      BindingOperations.SetBinding(contentcontrol, ContentControl.ContentProperty, binding); 
     } 

     public void UpdateBinding(FrameworkElement contentcontrol, object datacontext) 
     { 
      binding = new Binding(); 
      binding.Path = new PropertyPath("Profession2"); 
      binding.Mode = BindingMode.TwoWay; 
      binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
      binding.Source = datacontext; // view model 
      BindingOperations.SetBinding(contentcontrol, ContentControl.ContentProperty, binding); 
     } 

     private void Button_Click_2(object sender, RoutedEventArgs e) 
     { 
      var datacontext = (this.Resources["datacontent"] as MainPageViewModel); 
      this.UpdateBinding(this.contentControl,datacontext); 
     } 
} 

回答

0

之后,在运行时将ContentControl的DataContext更改为Profession2。

绑定路径从Profession1更新为Profession2。并更新ComboBoxItemsSource。但是由于您将Profession1Profession2定义为相同的值,因此它看起来像绑定源不会更新。请将它们设置为不同的值以检查结果。例如,更新您的MainViewModel如下:

public class MainPageViewModel 
{ 
    List<string> Profession1list=new List<string>() { "Lawyer", "Politician", "Other" }; 
    List<string> Profession2list = new List<string>() { "Lawyer2", "Politician2", "Other2" }; 
    public MainPageViewModel() 
    { 
     Profession1 = new Person(Profession1list); 
     Profession2 = new Person(Profession2list); 
    } 
     private Person profession1; 

    public Person Profession1 
    { 
     get { return profession1; } 
     set { this.profession1 = value; } 
    } 

    private Person profession2; 

    public Person Profession2 
    { 
     get { return profession2; } 
     set { this.profession2 = value; } 
    } 

    ... 

} 

public class Person : INotifyPropertyChanged 
{ 
    public Person(List<string> Professionlist) 
    { 
     _professions = new List<string>(); 
     foreach(string item in Professionlist) 
     { 
      _professions.Add(item); 
     } 

     //_professions.Add("Lawyer"); 
     //_professions.Add("Politician"); 
     //_professions.Add("Other"); 
    }  

    private List<string> _professions; 

    public List<string> Professions 
    { 
     get 
     { 
      return _professions; 
     } 
    } 
    ... 

} 

在这种情况下,组合框的DataContext没有改变,也被设置为空值的SelectedItem以前的DataContext。

由于ComboBoxItemsSource被重新绑定更新,所以SelectedItem清除。

我的测试环境是OS版本15063.更多信息请参考Data binding overview。如果您详细说明了您用于此功能的场景,我们可能会为此提供更好的实践。例如,为了避免更新绑定路径以满足要求,但是通过其他方式。

+0

HI Sunteen,感谢您的更新。我提出了一个与此查询相关的更多问题https://stackoverflow.com/questions/46112424/comboboxprevious-datacontext-selecteditem-property-set-as-null-value-in-uwp请在此分享您的想法。 – Srinivasan