首页 文章

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

提问于
浏览
1

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

但是我现在需要它来处理UserControls ViewModel的值 . 我可以使用INotifyPropertyChanged成功更新UserControl UI,但我无法将此值绑定到主窗口中的TabItem控件,因为它似乎对它进行了重新命名 .

这甚至可能还是我在错误的树上吠叫?

主窗口(TabControl)<---> UserControl <---> ViewModel

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 回答

  • 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'(Name ='uc1')'上找不到'SomeFigureVM'属性 . BindingExpression:路径= SomeFigureVM; DataItem ='UserControl1'(Name ='uc1'); target元素是'Label'(Name =''); target属性是'Content'(类型'Object')

相关问题