因此,如前面提到的問題中,我問WPF Data Binding From UserControl我有成功的基礎綁定了我的控件的TabHeader基於我的UserControls代碼中的值使用DependencyProperty後面的文件,並實現了一個類似用INotifyPropertyChanged實現。WPF從UserControl視圖模型綁定主窗口控件
但是我現在需要它來處理UserControls ViewModel的值。我可以使用INotifyPropertyChanged成功更新UserControl UI,但我無法將此值綁定到主窗口中的TabItem控件,因爲它似乎將其重新生成。
這是甚至可能還是我吠錯了樹?
主窗口(的TabControl)< --->用戶控件< --->視圖模型
MainWindow.xaml
<Grid>
<TabControl Height="250" HorizontalAlignment="Left" Margin="12,26,0,0" Name="tabControl1" VerticalAlignment="Top" Width="479">
<TabControl.Resources>
<Style TargetType="TabItem" x:Key="tab1ItemHeaderStyle" >
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate DataType="{x:Type TabItem}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=Header, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"/>
<Label Content="{Binding Path=SomeFigureVM, ElementName=uc1}"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Style="{StaticResource tab1ItemHeaderStyle}" Header="[Tab 1]" Name="tabItem1">
<vw:UserControl1 x:Name="uc1"></vw:UserControl1>
</TabItem>
</TabControl>
</Grid>
UserControl1.xaml
<Grid>
<Label Height="43" HorizontalAlignment="Left" Margin="69,128,0,0" Name="textBlock" Content="{Binding SomeFigureVM}" VerticalAlignment="Top" Width="100" />
<Button Name="updateSomeFigure" Content="Update.." Click="updateSomeFigure_Click" Width="100" Height="100" Margin="69,12,66,71" />
</Grid>
UserControl1.xaml.cs
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = new MyViewModel();
}
private void updateSomeFigure_Click(object sender, RoutedEventArgs e)
{
MyViewModel viewmodel = this.DataContext as MyViewModel;
viewmodel.UpdateFigure();
}
}
個
MyViewModel.cs
public class MyViewModel: INotifyPropertyChanged
{
public MyViewModel()
{
this.SomeFigureVM = 23;
}
private int _someFigure;
public int SomeFigureVM
{
get
{
return _someFigure ;
}
set
{
_someFigure = value;
NotifyPropertyChanged("SomeFigureVM");
}
}
public void UpdateFigure()
{
SomeFigureVM = SomeFigureVM + 1;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
與往常一樣,任何幫助是極大的讚賞,我覺得我已經砸我的頭撞牆就這一個!
[不要將DataContext硬編碼到UserControl中](http://stackoverflow.com/a/16488618/302677)。它擊敗了WPF擁有獨立UI和數據層的最大好處之一。如果你有興趣,我有一篇博客文章可以幫助你更好地理解WPF的'DataContext':[你說的這個「DataContext」是什麼?](http://rachel53461.wordpress.com/2012/07/14/what-this-this-datacontext-you-speak-of /) – Rachel
謝謝你的鏈接!真的很好的建議! – metoyou