簡體   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