【问题标题】:WPF Binding a Main Window Control from a UserControl View ModelWPF 从 UserControl 视图模型绑定主窗口控件
【发布时间】:2013-12-13 00:59:09
【问题描述】:

所以正如我之前问过WPF Data Binding From UserControl 的问题中提到的那样,我已经使用 DependencyProperty 根据我的 UserControls 代码隐藏文件中的值成功绑定了我的控件的 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));
            }
        }
    }

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

【问题讨论】:

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


【解决方案1】:

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

改变这一行:

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

到

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

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

System.Windows.Data 错误:40:BindingExpression 路径错误: 在“对象”“UserControl1”上找不到“SomeFigureVM”属性 (名称='uc1')'。 BindingExpression:Path=SomeFigureVM; DataItem='UserControl1' (名称='uc1');目标元素是“标签” (名称='');目标属性是“内容”(类型“对象”)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多