我在ContentControl
的ContentTemplate
中使用了ComboBox
。最初,我将DataContext
(Profession1
)与内容属性ContentControl
绑定在后面的代码中。ComboBox DataContex未更新放置在ContentControl(UWP)中的属性?
然后我在运行时更改ComboBox
的值。之后,在运行时将ContentControl
的DataContext
更改为Profession2
。在这种情况下,ComboBox
DataContext
未更改,前面的DataContext
的SelectedItem
值设置为空。
我认为这是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);
}
}
HI Sunteen,感谢您的更新。我提出了一个与此查询相关的更多问题https://stackoverflow.com/questions/46112424/comboboxprevious-datacontext-selecteditem-property-set-as-null-value-in-uwp请在此分享您的想法。 – Srinivasan