簡體   English   中英

無法在用戶控件上顯示綁定?

[英]Cannot display binding on usercontrol?

我無法在用戶控件上顯示屬性值。 我以這種方式設置了數據上下文:

public MainController cm;
public static MainWindow AppWindow;

public partial class MainWindow
{
     public MainWindow()
     {
        InitializeComponent();
        cm = new MainController();
        DataContext = cm;

        AppWindow = this;
     }
}

MainController內部,我具有所有具有以下所有屬性的控制器:

public class MainController: MainControllerVM
{
    private ClubController _clubController = new ClubController();

    public ClubController ClubController 
    {
        get { return _clubController ; }
    }
}

現在,我已將用戶界面拆分為不同的控件,以擁有更多的xaml組織。 我需要從所有用戶控件訪問cm的主數據上下文,我以這種方式嘗試過:

public partial class Club : UserControl
{
    public Club ()
    {
        InitializeComponent();
        DataContext = MainWindow.AppWindow.cm;
    }

但我得到:

NullReferenceException

在AppWindow上。 我的主要問題是我無法在用戶控件上可用的標簽上顯示屬性的值:

<Label Content="{Binding ClubController.Club.Name}" />

此綁定在主窗口中起作用,但在usercontrol上不起作用,為什么?

假設您有一個這樣的窗口:

<Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example"
        Title="MainWindow" Height="350" Width="525">
    <UniformGrid Rows="2" Columns="2">
        <local:MyUserControlA/>
        <local:MyUserControlB/>
        <local:MyUserControlC/>
        <local:MyUserControlD/>
    </UniformGrid>
</Window>

然后在構造函數中設置DataContext:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}

現在請記住,DataContext是可繼承的依賴項屬性,即它向下流動。 (通常,除非您明確聲明,否則依賴項屬性默認情況下不可繼承)

因此,只需將DataContext設置在邏輯樹(窗口)的根上,其所有子級都將“看到”它。 (本例中為UniformGrid和自定義控件)

是的,這意味着您可以直接在用戶控件的XAML中綁定到視圖模型:

<UserControl x:Class="Example.MyUserControlA"
             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">
    <Grid>
        <TextBlock Text="{Binding PropertyFromMainViewModel}"/>      
    </Grid>
</UserControl>

現在,這種方法行之有效,直到您的控件變得如此復雜,以至於需要分別擁有自己的ViewModel和DataContext。 通常,當控件不是被動控件而是保持狀態(驗證輸入,按鈕狀態等)時,會發生這種情況

1.將要綁定到主視圖模型的所有屬性聲明為依賴項屬性,並注意指定的默認值。

2.找到您的UserControl的主面板並命名,例如“ LayoutRoot”:

 <UserControl x:Class="Example.MyUserControlA"
             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">
    <Grid x:Name="LayoutRoot">
        <TextBlock Text="{Binding MyDependencyProperty}"/>      
    </Grid>
</UserControl>

3.現在,您在LayoutRoot上設置DataContext

public MyUserControlA()
{
    InitializeComponent();
    LayoutRoot.DataContext = new MyUserControlViewModel();
}

4.您以這種方式綁定到主視圖模型

 <Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example"
        Title="MainWindow" Height="350" Width="525">
    <UniformGrid Rows="2" Columns="2">
        <local:MyUserControlA MyDependencyProperty="{Binding MainViewModelProperty}"/>
        <local:MyUserControlB/>
        <local:MyUserControlC/>
        <local:MyUserControlD/>
    </UniformGrid>
</Window>

另一種方法是使用RelativeSource進行綁定,但這會破壞UserControl的封裝和可重用性。

WPF的學習曲線很陡,希望我的技巧對您有所幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM