绑定到基类 [英] Binding to base class

查看:54
本文介绍了绑定到基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有两个依赖于基类的局部类,并且正在尝试通过xaml将其窗口绑定到基类.当我独立于基类进行绑定时,使用的语法是relativesource self.在基类的情况下,我该如何实现.

下面是独立于基类的绑定语法,效果很好.

Hello,

I have two partial classes dependent on a base class and am attempting to bind their windows to the base class via xaml. When I bind independent of the base class, the syntax used is relativesource self. How can I achieve this in the case of the base class.

Below is the syntax for binding independent of the base class which works fine.

DataContext="{Binding RelativeSource={RelativeSource Self}}"




谢谢




Thanks

推荐答案

您使用的是同一层次结构吗?这很好.注意我正在使用您的DataContext绑定到self,并同时从基类和派生类访问属性...

Is this the same hierarchy you are using? This works fine. Note I''m using your DataContext binding to self, and accessing properties from both the base and the derived classes...

namespace WPFTester
{
    public class BaseClass : Window
    {
        public string BaseClassString { get; set; }

        public BaseClass()
        {
            BaseClassString = "BaseClassString";
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : BaseClass
    {
        public string DerivedClassString { get; set; }

        public MainWindow()
        {
            DerivedClassString = "DerivedClassString";
            InitializeComponent();
        }
    }
}





<local:BaseClass x:Class="WPFTester.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFTester"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}" >
    <Grid>
        <StackPanel >
            <TextBlock Text="{Binding Path=BaseClassString}" />
            <TextBlock Text="{Binding Path=DerivedClassString}" />
        </StackPanel>
    </Grid>
</local:BaseClass>


这篇关于绑定到基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆