0

我的项目代码中有两个自定义 UserControl:TableControl 和 DeckControl。在后者的代码中,我可以在需要时访问前者。所以在我的 DeckControl 中,我实现了以下属性:

private TableControl m_Table;

public TableControl Table
{
    get { return m_Table; }
    set { m_Table = value; }
}

问题是我无法从 XAML 代码设置属性:

<Canvas Core:Name="Layout" Loaded="OnLayoutLoaded">
    <Namespace:TableControl Core:Name="Table" Canvas.Left="0" Canvas.Top="0" Height="{Binding ElementName=Layout, Path=ActualHeight}" Width="{Binding ElementName=Layout, Path=ActualWidth}"/>
    <Namespace:DeckControl Core:Name="Deck" Canvas.Left="50">
</Canvas>

我尝试使用 Reference 但编译器说该方法或操作

<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50" Table="{Core:Reference Name=Table}">

我试过这个,但它也不起作用:

<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50" Table="{Core:Static Table}">

我也尝试使用绑定:

<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50" Table="{Binding ElementName=Table}">

好吧……这是我第一次使用 XAML 的方法,我仍在努力……但我真的无法理解!

4

1 回答 1

1

如果要绑定到模型(窗口/用户控件)代码隐藏中的属性,则必须DataContextXaml. 有很多方法可以做到这一点,但最简单的方法就是命名您的窗口或用户控件并使用ElementName.

窗口示例:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="233" Width="143" Name="UI">

    <Canvas DataContext="{Binding ElementName=UI}" > <!-- Set dataContext to Window -->
        <Namespace:DeckControl Canvas.Left="50" Table="{Binding ElementName=Table}">
    </Canvas>
</Window>

如果您希望Xaml在 Table 更改您的代码后应该实现时更新INotifyPropertyChanged,这将通知Xaml属性已更改。

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private TableControl m_Table;

    public TableControl Table
    {
       get { return m_Table; }
       set { m_Table = value; NotifyPropertyChanged("Table"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

如果您的 Table 属性不是 DependancyProperty,您将不得不更改它以便可以绑定。

例子:

public class DeckControl : UserControl
 {
     .......



     // Using a DependencyProperty as the backing store for Table.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty TableProperty =
         DependencyProperty.Register("Table", typeof(TableControl), typeof(DeckControl), new UIPropertyMetadata(null));

     public TableControl Table
     {
         get { return (TableControl)GetValue(TableProperty); }
         set { SetValue(TableProperty, value); }
     }
 }

此外,在 UserControl 范围之外绑定的任何属性都必须是 DependancyProperty。

例子:

public partial class DeckControl : UserControl
{
    public DeckControl()
    {
        InitializeComponent();
    }

    private int myVar;
    public int MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }
}

当它是一个简单属性时,它将绑定在用户控件内部,因为它是在范围内。

<UserControl .....
             d:DesignHeight="300" d:DesignWidth="300" Name="UI">

    <TextBlock Text="{Binding MyProperty}" />
</UserControl>

这不会绑定,因为它超出了 UserControl 的范围,MyProperty 必须是 DependancyProperty 才能在此处绑定

<Window ....
        Title="MainWindow" Height="233" Width="143" Name="UI">

    <Grid>
        <local:DeckControl MyProperty="{Binding Width}"  /> // Will not bind
    </Grid>

</Window>

希望这是有道理的:)

于 2013-01-27T21:05:02.717 回答