繁体   English   中英

XAML绑定到UserControl中的FrameworkElement

[英]XAML Binding to a FrameworkElement in a UserControl

我得到了具有几个属性的自定义UserControl(MyControl)(效果很好)。 我想要一个新属性,使使用UserControl的页面“粘贴”某些内容直接在UserControl-ex中显示。 一条路径。 我努力了; ContentPresenter,ContentControl,StackPanel,没有运气...

MyControl.xaml

<ContentControl Grid.Column="0" Content="{Binding MyContent, ElementName=root}"></ContentControl>

MyControl.xaml.cs

public object MyContent
{
  get { return (object)GetValue(MyContentProperty); }
  set { SetValue(MyContentProperty, value); }
}

public static readonly DependencyProperty MyContentProperty =
  DependencyProperty.Register("MyContent", typeof(object), typeof(MyControl), new PropertyMetadata(null));

SomePage.xml

<mycontrols:MyControl x:Name="FavoritesButton">
  <mycontrols:MyControl.MyContent>
    <Path Data="M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z" Stretch="Uniform" Fill="#FFFFFFFF" Width="50" Height="50" Margin="30"></Path>
  </mycontrols:MyControl.MyContent>
</mycontrols:MyControl>

我有以下工作真的很好。 (尽管这在某种程度上违反了MVVM的原理……我仍然想在主窗口的单个框架区域中动态处理用户控件)

我的MainWindow.xaml:

<!-- Main Frame -->
<Grid Grid.Column="1" Margin="10" Name="MainWindowFrameContent">
   <ItemsControl ItemsSource="{Binding Path=MainWindowFrameContent}" >

      <!-- This controls the height automatically of the user control -->
      <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <UniformGrid Columns="1" IsItemsHost="True"/>
         </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
   </ItemsControl>
</Grid>

我的MainViewModel.cs:

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using myProject.View;
using myProject.Models;

namespace myProject.ViewModel
{
    public class MainViewModel : ObservableObject
    {
        public MainViewModel() { }

        // This handles adding framework (UI) elements to the main window frame
        ObservableCollection<FrameworkElement> _MainWindowFrameContent = new ObservableCollection<FrameworkElement>();
        public ObservableCollection<FrameworkElement> MainWindowFrameContent
        {
            get { return _MainWindowFrameContent; }
            set { _MainWindowFrameContent = value; RaisePropertyChangedEvent("MainWindowFrameContent"); }
        }
    }
}

MainViewModel.cs是一个“公共类MainViewModel:ObservableObject”。 这使我可以实现“ RaisePropertyChangedEvent”,以便当我更改“ MainWindowFrameContent”的值时绑定将成功更新。

我的ObservableObject.cs:

using System.ComponentModel;

namespace myProject.ViewModel
{
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChangedEvent(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

然后,当我想向MainWindowFrameContent添加项目时,只需在MainViewModel.cs中执行以下操作:

void _AddNewUserControl()
{
    myUserControl hControl = new myUserControl();
    MainWindowFrameContent.Clear(); // Clear the frame before displaying new content
    MainWindowFrameContent.Add(hControl);
}

然后,您可以创建任意数量的用户控件。 您要显示的每个命令在VM中都可以有其自己的void _AddNewUserControl类型方法,并将显示在主窗口中。 再次,这有点与MVVM框架相反,但是它使代码库中的内容保持整洁。

我让它工作了...解决方案如下:

MyControl.xaml

<ContentControl Content="{Binding Shape, ElementName=root}" />

MyControl.xaml.cs

public Shape Shape
{
    get { return (Shape)GetValue(ShapeProperty); }
    set { SetValue(ShapeProperty, value); }
}

public static readonly DependencyProperty ShapeProperty =
        DependencyProperty.Register("Shape", typeof(Shape), typeof(MyControl), new PropertyMetadata(null));

SomePage.xml

<mycontrols:MyControl>
    <mycontrols:MyControl.Shape>
        <Path Data="M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z" Style="{StaticResource PathStyle}" />
    </mycontrols:MyControl.Shape>
</mycontrols:MyControl>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM