簡體   English   中英

依賴屬性未綁定到視圖

[英]Dependency property not binding to view

我想做的是根據樹狀視圖的選定項填充一個組合框。 因為根據選擇的節點不同,組合框將填充不同級別的報告級別。

該組合框在用戶控件內,並且我試圖綁定到MainViewModel中具有的依賴項屬性ReportLevel。 如果我將組合框的值設置為正常,但我希望每當用戶在樹上選擇其他節點時都能夠對其進行更新。

這是我的xml

    <UserControl x:Name="this"
    x:Class="RTHM.ComboboxControl1"
         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" 
         xmlns:local ="clr-namespace:RTHM.ViewModels"
         d:DesignHeight="300" d:DesignWidth="300">

<Grid>
    <StackPanel HorizontalAlignment="Center" MinHeight="221.904" Margin="12,12,42,0" VerticalAlignment="Top" MaxWidth="246.226" Width="246">
        <WrapPanel HorizontalAlignment="Left" MinHeight="224.072" VerticalAlignment="Top" MinWidth="246.13">
            <TextBox HorizontalAlignment="Left" MinHeight="104.489" Margin="10,289.95,0,0" TextWrapping="Wrap" Text="{Binding ReportDescription, Mode=TwoWay}" VerticalAlignment="Top" MinWidth="245.124"/>
            <TextBox MinHeight="23" TextWrapping="Wrap" Text="Report Level:" MinWidth="120" BorderThickness="1" Margin="0,0,5,5"/>
            <ComboBox MinWidth="120" Margin="0,0,0,5" MinHeight="23" 

                      ItemsSource="{Binding ElementName=this, Path=ReportLevel,Mode=TwoWay}" 
                      IsSynchronizedWithCurrentItem="True"
                      DisplayMemberPath="Name" 
                      SelectedItem="{Binding Path=SelectedLevel, Mode=TwoWay}"
                      IsEnabled="{Binding IsEnabled}"/>

我的代碼在后面

public partial class ComboboxControl1 : UserControl
{
    public ComboboxControl1()
    {
        InitializeComponent();

       DataContext = new MainViewModel();

在我的MainViewModel中,我有這個

    #region DependencyProperties
   public static readonly DependencyProperty LevelProperty =
       DependencyProperty.Register("ReportLevel",typeof(ObservableCollection<ReportLevel>),typeof(MainViewModel));

    public ObservableCollection<ReportLevel> ReportLevel
   {
       get
       {
           return (ObservableCollection<ReportLevel>)GetValue(LevelProperty);
       }
       set
       {
           SetValue(LevelProperty, value);
       }
   }

並有一個方法可以像這樣設置值

            ReportLevel = c.GetUserReportLevel();

我已經用組合框的ItemSource嘗試了各種方法,但是沒有成功。

在這個問題上的任何幫助將不勝感激

謝謝,馬蒂(Marty)編輯:更新了我的代碼,但是還是沒運氣,有什么想法嗎? XAML

<UserControl x:Class="RTHM.ComboboxControl1"
         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" 
         xmlns:local ="clr-namespace:RTHM.ViewModels"
         d:DesignHeight="300" d:DesignWidth="300">

<Grid>
    <StackPanel HorizontalAlignment="Center" MinHeight="221.904" Margin="12,12,42,0" VerticalAlignment="Top" MaxWidth="246.226" Width="246">
        <WrapPanel HorizontalAlignment="Left" MinHeight="224.072" VerticalAlignment="Top" MinWidth="246.13">
            <TextBox HorizontalAlignment="Left" MinHeight="104.489" Margin="10,289.95,0,0" TextWrapping="Wrap" Text="{Binding ReportDescription, Mode=TwoWay}" VerticalAlignment="Top" MinWidth="245.124"/>
            <TextBox MinHeight="23" TextWrapping="Wrap" Text="Report Level:" MinWidth="120" BorderThickness="1" Margin="0,0,5,5"/>
            <ComboBox MinWidth="120" Margin="0,0,0,5" MinHeight="23" 

                      ItemsSource="{Binding Path=Levels}" 
                      IsSynchronizedWithCurrentItem="True"
                      DisplayMemberPath="Name" 
                      SelectedItem="{Binding Path=SelectedLevel, Mode=TwoWay}"
                      IsEnabled="{Binding IsEnabled}"/>

和在MainViewModel中

private ObservableCollection<ReportLevel> _levels;
public ObservableCollection<ReportLevel> Levels
    {
        get
        {
            return _levels;
        }
        set
        {
            _levels = value;
            NotifyPropertyChanged("Levels");
        }
    }

我的MainViewModel繼承自具有INotifyProperty更改和實現的基類

public class ViewModelBase :  INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

綁定

ItemsSource="{Binding ElementName=this, Path=ReportLevel, Mode=TwoWay}" 

綁定到您的UserControl中的ReportLevel屬性。 但是,沒有這樣的屬性,因為它位於UserControl的DataContext中。 綁定應如下所示。 另請注意,對ItemsSource屬性進行雙向綁定是沒有意義的,因為該控件從不設置該屬性。

ItemsSource="{Binding Path=ReportLevel}" 

也就是說,您通常在視圖模型中沒有依賴項屬性。 因此,視圖模型類應實現INotifyPropertyChanged接口。

ReportLevel屬性更改時,您還必須實際觸發PropertyChanged事件:

private ObservableCollection<ReportLevel> reportLevel;
public ObservableCollection<ReportLevel> ReportLevel
{
    get { return reportLevel; }
    set
    {
        reportLevel = value;
        OnPropertyChanged("ReportLevel");
    }
}

暫無
暫無

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

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