我有一个动态加载图的需求(XAML,包括自定义UserControl,ScrewControl
)。
我正在研究从ViewModel类绑定ScrewControl
的依赖属性(Status
)的方法。挑战在于,一个图可能包含不同数量的ScrewControl
,每个ScrewControl
都需要绑定到ViewModel
中的字典或集合。
我已经解决了使用XamlReader
动态加载XAML的第一个挑战,但不确定如何将ScrewControl
与ViewModel中的Collection/dictionary绑定。
视图模型类
public class AssemblyViewModel
{
public Dictionary<int, ScrewStatus> Screws { get; set; }
public FrameworkElement Image { get; set; }
...
}
视图类
public partial class AssemblyView : UserControl
{
private AssemblyViewModel viewModel;
public AssemblyView(AssemblyViewModel viewModel)
{
InitializeComponent();
DataContext = this.viewModel = viewModel;
Loaded += OnLoaded;
}
public FrameworkElement XamlContent
{
get { return (FrameworkElement)GetValue(XamlContentProperty); }
set { SetValue(XamlContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty XamlContentProperty =
DependencyProperty.Register("XamlContent", typeof(FrameworkElement), typeof(AssemblyView), new PropertyMetadata(null, OnXamlChanged));
private static void OnXamlChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var view = dependencyObject as AssemblyView;
var element = args.NewValue as FrameworkElement;
if (null != element)
{
view.ContentControl.Children.RemoveRange(0, 1);
view.ContentControl.Children.Add(element);
var screws = element.FindChildren<Screw>();
try
{
foreach (var screw in screws)
{
// The ScrewControl has a name properties like "Screw_1", 1 is used as the key of the dictionary and Status as the Value
var parts = screw.Name.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
if (2 == parts.Length)
{
BindingOperations.SetBinding(
screw,
Screw.ScrewStatusProperty,
new Binding(/*not sure what goes here*/)
{
Source = view.viewModel.Screws,
});
}
}
}
catch (Exception e)
{
throw;
}
}
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
// This works and loads the diagram
BindingOperations.SetBinding(
this,
XamlContentProperty,
new Binding("Image")
{
Source = this.viewModel
});
}
1条答案
按热度按时间nvbavucw1#
你就快成功了。这应该可以工作:
虽然从XAML加载的Screw控件可能更像WPF,但它是一个公共属性,并将其绑定为ItemsControl的ItemsSource。然后,您可以将ItemTemplate设置为ContentControl,其中Content属性绑定到ScrewStatus,ContentTemplate绑定到当前项(即从XAML加载的screw用户控件),这将提供一种更自然的方式将视图和模型绑定在一起。