在WPF项目中,我使用MVVM模式。
我尝试将集合中的项目绑定(bind)到UserControl,但是所有内容都将获得DependcyProperty的默认值。

窗口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>

session 在哪里
 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();

我总是在default_from_control中获得DebugControlTextBlock值,但是在DebugTextBlock中获得the_real_connection c# - 无法从WPF中的集合绑定(bind)模型的DependcyProperty-LMLPHP

即使我在DebugConverter中设置断点,我也会看到该值为defaultDebugConverter只是包装器,用于检查正确的绑定(bind):
 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的解决方案。
那么,当我将模型绑定(bind)到DependcyProperty时会发生什么?

最佳答案

我建议您也尝试使Sessions属性也成为DependencyProperty。否则,您可能必须手动为Sessions属性设置PropertyChanged。

关于c# - 无法从WPF中的集合绑定(bind)模型的DependcyProperty,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34261400/

10-11 12:24
查看更多