将对象绑定到UserControl

时间:2016-04-04 10:45:20

标签: c# wpf xaml data-binding user-controls

我正在尝试从我的主UserControl控制子UserControls的状态。我想通过在Parent UC中创建DependencyProperty对象并在创建Children UC时在XAML中引用它来实现这一目标。

问题是,我在Step.xaml.cs(后面的子代码)中得到NullReferenceException,其中所有内容都引用了Controller属性,首先是StepEnabled属性。当我省略XAML绑定并让它在构造函数中实例化时,它没关系,虽然我当然不能控制任何东西,但它的控制器属性的方式总是为空...代码如下:

ParentViewModel:

public class ParentVM : NotifyPropChanged
{
    ...

    public StepController Step1 = new StepController();
    public StepController Step2 = new StepController();
    public StepController Step3 = new StepController();

    public ParentVM()
    {
        ...

        GetReady();
    }

    public void GetReady()
    {
        Step1.Clear();
        Step2.Clear();
        Step3.Clear();

    }

StepController:

public class StepController
{
    public bool StepEnabled;
    public bool? WasSuccessful;

    public StepController()
    {
        Clear();
    }
    public void Clear()
    {
        StepEnabled = false;
        WasSuccessful = null;
    }
}

儿童(代码背后):

public partial class Step : UserControl
{
    public static DependencyProperty ControllerProperty =
    DependencyProperty.Register("Controller", typeof(StepController), typeof(Step), new UIPropertyMetadata(null));

    public StepController Controller
    {
        get { return (StepController)GetValue(ControllerProperty); }
        set { SetValue(ControllerProperty, value); }
    }

    ...

    public bool StepEnabled
    {
        get { return (bool)Controller.StepEnabled; }
        set { Controller.StepEnabled = value; }
    }
    public bool IsDone
    {
        get
        {
            return (Controller.WasSuccessful != null);
        }
    }
    public bool? WasSuccessful
    {
        get { return Controller.WasSuccessful; }
        set { Controller.WasSuccessful = value; }
    }

    public Step()
    {
        InitializeComponent();
        this.DataContext = this;

        ...

        Controller = new StepController();

    }

}

ParentUC:

...
<local:Step Controller="{Binding Path=Step1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
...

ChildUC:

e.g.:
<TextBlock IsEnabled="{Binding StepEnabled, UpdateSourceTrigger=PropertyChanged}" />

我是WPF的新手,我仍然不确定DataContext等等很多东西,所以我想我错过了一些明显的东西......感谢您提前获得的帮助!

3 个答案:

答案 0 :(得分:0)

不清楚你想用Controller做什么=&#34; {Binding Path = Step1,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}

首先,您创建了Controller作为Dependency属性,稍后您将在步骤构造函数中实例化相同属性。由于你是新手,你的概念看起来很乱,看到你的代码。 能否请您详细说明您想要实现的目标,以便我们为您提供帮助?只需用清晰的英语指定你想要的东西?

答案 1 :(得分:0)

绑定不适用于字段。

声明属性并重命名字段

svg { width: 100%; height: auto;}

您的异常可能是因为您正在使用&#34;控制器&#34;属性没有测试它为null。

此外,在你的&#34;步骤&#34;你做的构造

<svg viewbox="0 0 100 50">
  <path fill="#0081C2" d="M0 0 H100 V10 A85 85 1 0 1 0 10z" />
</svg>

如果您的目标是设置默认值,请通过

完成静态DP声明
 public StepController Step1 { get { return _step1; } set { _step1 = value; }

答案 2 :(得分:0)

就像@Synapse已经提到\中的DependencyProperty - Step应该为您的UserControl类设置默认值:

StepController

您的ParentUC应如下所示(DataContext仅在此处设置):

public static readonly DependencyProperty ControllerProperty = DependencyProperty.Register(
        "Controller", typeof (StepController), typeof (Step), new PropertyMetadata(default(StepController)));

    public StepController Controller
    {
        get { return (StepController) GetValue(ControllerProperty); }
        set { SetValue(ControllerProperty, value); }
    }

您的Child UserControl XAML是这样的:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:StepControllerExample" x:Class="StepControllerExample.ParentUserControl"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
    <local:ParentVM/>
</UserControl.DataContext>
<StackPanel HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100">
    <local:Step Controller="{Binding Step1}"/>
    <local:Step Controller="{Binding Step2}"/>
    <local:Step Controller="{Binding Step3}"/>
</StackPanel>

您的Child UserControl CodeBehind就是这样:

<UserControl x:Name="userControl" x:Class="StepControllerExample.Step"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical" d:LayoutOverrides="Height">
    <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="TextBlock"/>

    <CheckBox x:Name="checkBox" Content="CheckBox" IsChecked="{Binding Controller.StepEnabled, ElementName=userControl}"/>

</StackPanel>

和您的public partial class Step : UserControl { public Step() { InitializeComponent(); } public static readonly DependencyProperty ControllerProperty = DependencyProperty.Register( "Controller", typeof (StepController), typeof (Step), new PropertyMetadata(default(StepController))); public StepController Controller { get { return (StepController) GetValue(ControllerProperty); } set { SetValue(ControllerProperty, value); } } } 类一样:

StepController