我試圖創建一個「ModalPopup」自定義控制,具有以下特點:WPF自定義控件與兒童項目
- 它有一個主要內容區
Content
是始終顯示。 - 它有一個子集合
PopupItems
項目(模態地)顯示在主內容區域頂部,半透明邊框覆蓋主內容區域。 - 如果有任何子項目的
IsShown
屬性設置爲true,則會顯示它們,主內容區域將被禁用。
我已經得到這個工作,但我不斷收到錯誤,像A value of type ‘ModalPopupItem’ cannot be added to a collection or dictionary of type ‘ObservableCollection’.
我使用UIElementCollection,列表,列表嘗試了設計師,我仍然不斷收到錯誤。此外,設計者並沒有反映我對xaml所做的任何更改,直到我重新編譯,這是非常令人沮喪的。
我能做些什麼來讓設計師停止發生這些錯誤?
ModalPopup:
public class ModalPopup: ContentControl
{
public ModalPopup()
{
this.PopupItems = new ObservableCollection<ModalPopupItems>();
this.Loaded += ModalPopup_Loaded;
}
private void ModalPopup_Loaded(object sender, RoutedEventArgs e)
{
foreach (ModalPopupItem item in this.PopupItem)
{
Item.IsShownChanged += Item_IsShownChanged;
}
UpdateOverlay();
}
public static readonly DependencyProperty ShowOverylayProperty =
DependencyProperty.Register.(「ShowOverlay」, typeof(bool), typeof(ModalPopup), new PropertyMetadata(true));
public bool ShowOverlay
{
get {return (bool)GetValue(ShowOverlayProperty);}
set {SetValue(ShowOverlayProperty, value);}
}
public static readonly DependencyProperty PopupItemsProperty =
DependencyProperty.Register.(「PopupItems」, typeof(ObservableCollection<ModalPopupItem>), typeof(ModalPopup))
public ObservableCollection<ModalPopupItem>ShowOverlay
{
get {return (ObservableCollection<ModalPopupItem>)GetValue(ShowOverlayProperty);}
set {SetValue(ShowOverlayProperty, value);}
}
private void UpdateOverlay()
{
ShowOverlay = (PopupItems?.Any(item => item.IsShown) ?? false);
}
private void item_IsShownChanged(object sender, EventArgs e)
{
UpdateOverlay();
}
}
ModalPopupItem:
public class ModalPopupItem: ContentControl
{
public EventHandler IsShownChanged;
public static readonly DependencyProperty IsShownProperty =
DependencyProperty.Register(「IsShown」, typeof(bool), typeof(ModalPopupItem), new PropertyMetadata(true, IsShownChanged_Callback));
Public bool IsShown
{
get { return (bool)GetValue(IsShownProperty); }
set { SetValue(IsShownProperty, value); }
}
private static void IsShownChanged_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as ModalPopupItem).Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;
(d as ModalPopupItem).IsShownChanged?.Invoke(d, EventArgs.Empty);
}
}
資源文件:
<Style TargetType=」local:ModalPopup」>
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ModalPopup">
<Grid>
<ContentPresenter Content="TemplateBinding Content}"
IsEnabled="{TemplateBinding ShowOverlay, Converter={StaticResource InvertBool}}" />
<ContentControl Template="{StaticResource PopupTemplate}"
Visibility="{TemplateBinding ShowOverlay, Converter={StaticResource TrueToVisible}}">
<ItemsControl ItemsSource="{TemplateBinding PopupItems}" />
</ContentControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
這是我如何使用我的控制...。
<local:ModalPopup>
<!-- Main Content Area -->
<TextBlock Text="Main Content" Height=「200」 Width=」200」 />
<!-- Popup Items -->
<local:ModalPopup.PopupItems>
<local:ModalPopupItem IsShown="False">Popup 1</ local:ModalPopupItem>
<local:ModalPopupItem IsShown="True">Popup 2</ local:ModalPopupItem>
<local:ModalPopupItem IsShown="{Binding ShowPopup3}">Popup 3</ local:ModalPopupItem>
</local:ModalPopup.PopupItems>
</local:ModalPopup>