이전 질문에서 언급했듯이 WPF Data Binding From UserControl succesfulyl은 내 Control의 TabHeader를 DependencyProperty를 사용하여 파일 뒤에있는 UserControls 코드 내부의 값을 기반으로 바인딩했으며 유사한 aa를 사용했습니다. INotifyPropertyChanged를 사용한 구현.WPF UserControl에서 주 창 컨트롤 바인딩 뷰 모델
그러나 이제는 UserControls ViewModel에서 값을 처리해야합니다. 내가 INTifyPropertyChanged를 사용하여 UserControl UI를 성공적으로 업데이트 할 수 있지만이 값을 Regonise 것으로 보이는 MainItem의 TabItem 컨트롤에 바인딩 할 수 없습니다.
이 경우에도 가능합니까 아니면 잘못된 트리를 짖고 있습니까?
메인 윈도우 (을 TabControl) < ---> UserControl을 < ---> 뷰 모델
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). 별도의 UI 및 데이터 레이어가있는 WPF의 가장 큰 이점 중 하나입니다. 관심이 있으시면 WPF의'DataContext'에 대해 조금 더 잘 이해할 수있는 블로그 기사가 있습니다 : [당신이 말하는이 "DataContext"는 무엇입니까?] (http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of /) – Rachel
링크를 가져 주셔서 감사합니다! 정말 좋은 충고! – metoyou