2015-12-14 84 views
2

在WPF項目中,我使用MVVM模式。 我嘗試將集合中的項目綁定到UserControl,但所有內容均獲取默認值DependcyProperty無法綁定WPF中收集模型的依賴屬性

的窗口XAML:

<ListView VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                ItemsSource="{Binding Sessions}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Name="DebugTextBlock" Background="Bisque" 
         Text="{Binding Connection}"/> 
        <usercontrol:SessionsControl Model="{Binding Converter= 
        {StaticResource DebugConverter}}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

哪裏會議是

private ObservableCollection<SessionModel> _sessions; 

     public ObservableCollection<SessionModel> Sessions 
     { 
      get { return _sessions; } 
      set 
      { 
       if (Equals(value, _sessions)) return; 
       _sessions = value; 
       OnPropertyChanged("Sessions"); 
      } 
     } 

的SessionModel:

public class SessionModel:ViewModelBase 
{ 
    private string _connection; 

    public string Connection 
    { 
      get { return _connection; } 
      set 
      { 
       if (value == _connection) return; 
       _connection = value; 
       OnPropertyChanged("Connection"); 
      } 
    } 
} 

SessionsControl我創建DependencyProperty

//Dependency Property 
public static readonly DependencyProperty ModelProperty = 
     DependencyProperty.Register("Model", typeof(SessionModel), 
     typeof(SessionsControl), new PropertyMetadata(new SessionModel("default_from_control"))); 

// .NET Property wrapper 
public SessionModel Model 
{ 
    get { return (SessionModel)GetValue(ModelProperty); } 
    set { if (value != null) SetValue(ModelProperty, value); } 
} 

,並使用此XAML顯示在連接形式:

<TextBlock Name="DebugControlTextBlock" Background="Gray" Text="{Binding Connection}"/> 

所以,當我運行的應用程序

var windowModel = new WindowsModel(); 
var window = new SessionWindow(windowModel); 
window.ShowDialog(); 

我總是在DebugControlTextBlock得到default_from_control值,但在DebugTextBlock得到the_real_connection the window

即使我在中設置斷點我看到這個值是default。 的DebugConverter簡單包裝器來檢查正確的綁定:

public class DebugConverter:IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      Debug.WriteLine("DebugConverter: " + (value!=null?value.ToString():"null")); 
      return value; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return value; 
     } 
    } 

請參閱解決方案上github。 那麼,當我將模型綁定到DependcyProperty時會發生什麼?

+0

我們必須看看您的DebugConverter在這裏有什麼樣的幫助。如果您看到字符串Connection屬性的綁定工作,那麼您正在正確地進行綁定。 此外,您不需要使您的會話成爲依賴項屬性。當您使用MVVM時,ObservableCollection不需要像其他屬性那樣工作。當集合更改時,ObservableCollection會將其自己的通知提交給UI。在這種情況下,你的綁定只有在你正在傾聽整個集合被更改的情況下。 –

+0

我添加了'DebugConverter'的定義。我知道'Sessions'列表對於通知來說太胖了,稍後會使用'WindowsMo​​del'。 –

+0

看起來你只是回來了同樣的事情。您的調試WriteLine工作正常嗎?這可能是一個問題,因爲一旦模型被綁定到控件中,您將如何使用該模型。 'DebugConverter'中的 –

回答

0

我建議你試着讓Sessions屬性成爲DependencyProperty。否則,您可能必須手動爲會話屬性提升PropertyChanged。

+0

起初,我在初始化'Window'之前初始化'Sessions'的集合, 第二我已經爲'Sessions'提升PropertyChanged(請參閱已更正的主題)。 –