WPF从UserControl视图模型绑定主窗口控件

时间:2013-11-27 11:44:13

标签: wpf user-controls tabcontrol wcf-binding wpf-4.0

正如前面提到的问题中提到的那样WPF Data Binding From UserControl我使用DependencyProperty基于我的UserControls代码后面的值绑定了我的控件的TabHeader,并使用了INotifyPropertyChanged实现了类似的实现。

但是我现在需要它来处理UserControls ViewModel中的值。我可以使用INotifyPropertyChanged成功更新UserControl UI,但我无法将此值绑定到主窗口中的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));
            }
        }
    }

与往常一样,非常感谢任何帮助,我觉得我一直在这个墙上砸砖头!

1 个答案:

答案 0 :(得分:2)

SomeFigureVM是MyViewModel上的一个属性,DataContextUserControl1。您正在尝试访问UserControl上的SomeFigureVM属性,该属性不存在。

更改此行:

<Label Content="{Binding Path=SomeFigureVM, ElementName=uc1}"/>

<Label Content="{Binding Path=DataContext.SomeFigureVM, ElementName=uc1}"/>

要捕获这样的数据绑定错误,请在调试模式下运行应用程序,并观察输出窗口是否存在任何数据绑定问题。您的原始代码会生成数据绑定错误,如:

  

System.Windows.Data错误:40:BindingExpression路径错误:   在'object'''UserControl1'上找不到'SomeFigureVM'属性   (名称= 'UC1')”。 BindingExpression:路径= SomeFigureVM;   DataItem ='UserControl1'(Name ='uc1');目标元素是'标签'   (名称= ''); target属性是'Content'(类型'Object')