3
我已經從基類UserControl創建了一個自定義控件(我的第一個),現在我無法在進行將來的控件時從它繼承。繼承自定製控件
這裏是自定義控件模板
<Style TargetType="{x:Type local:LoadOverlayUserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LoadOverlayUserControl}">
<Grid>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"/>
<Border Visibility="{TemplateBinding OverlayVisibility}"
Background="{TemplateBinding OverlayColor}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<dxe:ProgressBarEdit />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
這是源代碼:
public class LoadOverlayUserControl : UserControl
{
static LoadOverlayUserControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LoadOverlayUserControl), new FrameworkPropertyMetadata(typeof(LoadOverlayUserControl)));
}
#region Overlay Visibility
public static readonly DependencyProperty OverlayVisibilityProperty = DependencyProperty.Register("OverlayVisibility",
typeof(Visibility),
typeof(LoadOverlayUserControl),
new PropertyMetadata(Visibility.Hidden));
private Visibility OverlayVisibility
{
get
{
return (Visibility)this.GetValue(OverlayVisibilityProperty);
}
set
{
this.SetValue(OverlayVisibilityProperty, value);
}
}
#endregion
#region Overlay Color
public static readonly DependencyProperty OverlayColorProperty = DependencyProperty.Register("OverlayColor",
typeof(Color),
typeof(LoadOverlayUserControl),
new PropertyMetadata(Color.FromRgb(0,0,0)));
public Color OverlayColor
{
get
{
return (Color)this.GetValue(OverlayColorProperty);
}
set
{
this.SetValue(OverlayColorProperty, value);
}
}
#endregion
public void SetLoading(bool IsLoading)
{
if (IsLoading)
{
OverlayVisibility = System.Windows.Visibility.Visible;
}
else
{
OverlayVisibility = System.Windows.Visibility.Hidden;
}
}
}
我碰到的問題是,當我從這種自定義控件繼承,它似乎忽略我的模板,最後我得到一個空的控件。
以下是繼承自此自定義控件的新控件的示例。
<custom:LoadOverlayUserControl x:Class "ChildControl"
...
...
...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../ResourceDictionaries/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
...
</Grid>
</custom:LoadOverlayUserControl>
這裏是我如何使用新的控件。爲了便於閱讀,我省略了命名空間,但我向你保證它們是正確的,除非我需要做一些棘手的事情。
<UserControl x:Class "SomeOtherControl">
<Grid>
<StackPanel>
<my:ChildControl />
</StackPanel>
</Grid>
</UserControl>
自定義控件背後的想法是,我希望能夠在與半透明的透明度上應用「目前正在加載」覆蓋控制。
請仔細閱讀[常見問題]並[問] –
我想過爲這個問題寫一個答案,但後來我 –
我們走了,下次你問問題的時候一定要確保在你寫完問題之前不要發帖,得到一堆downvotes。 –