0

在我的項目中,許多視圖必須具有Ok,Cancel按鈕才能查看底部。所以我想創建一個基礎控件。然後在此控件的底部添加確定,取消按鈕。然後我會繼承這個控制。在繼承的控件中,我想在文本框附近添加這個按鈕。我怎樣才能做到這一點?如何在Silverlight中使用派生控件的內容

+0

我不確定你要做什麼......你想在它和TextBlock後面的按鈕之後創建一個新的控制器嗎?Ok'或'Cancel'? –

回答

0

我找到了解決辦法。 [ContentProperty("")]屬性解決了我的問題。
像這樣:

基地的XAML

<Grid Name="layoutRoot" VerticalAlignment="Stretch" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="289*" /> 
      <RowDefinition Height="51" /> 
     </Grid.RowDefinitions> 
     <StackPanel Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Orientation="Horizontal"> 
      <Button Name="btnOk" Content="Ok" Height="23" Width="75" HorizontalAlignment="Left" Margin="10,10,10,10" Command="{Binding Path=OnApply, Mode=OneWay}"/> 
      <Button Name="btnCancel" Content="Cancel" Height="23" Width="75" HorizontalAlignment="Left" Margin="10,10,10,10"/> 
     </StackPanel> 
     <Grid Name="rootContent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" /> 
    </Grid> 
</sdk:ChildWindow> 

基地代碼隱藏

[ContentProperty("RootContentControl")]//This attribute solved my problem. 
public partial class BaseView : ChildWindow 
{ 

    public BaseView() 
    { 
     InitializeComponent(); 
    } 

    public UIElementCollection RootContentControl 
    { 
     get { return rootContent.Children; } 
    } 
} 

繼承的XAML

<Views:BaseView x:Class="MvvmLight1.Views.InheritedView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:Views="clr-namespace:MvvmLight1.Views" 
       xmlns:viewModels="clr-namespace:MvvmLight1.ViewModel"     
       mc:Ignorable="d" d:DesignHeight="244" d:DesignWidth="392" 
       DataContext="{Binding Source=viewModels:InheritedViewModel}"> 
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" > 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
    </Grid> 
</Views:BaseView> 

繼承代碼隱藏

public partial class InheritedView : BaseView 
{ 
    public InheritedView() 
    { 
     InitializeComponent(); 
    } 
} 
0

我相信有一個基本控件的模板。所以在繼承控件的模板中編輯按鈕面板和添加文本框。

0

您可以創建從ContentControl繼承的控件,並在ControlTemplate中定義您的按鈕。然後你可以使用這個控件,如示例中所示。 ControlWithButtons.cs

[TemplatePart(Name="btnOk", Type= typeof(Button))] 
public class ControlWithButtons : ContentControl 
{ 
    public ControlWithButtons() 
    { 
     this.DefaultStyleKey = typeof(ControlWithButtons); 
    } 

    Button _btnOk; 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     _btnOk = GetTemplateChild("btnOk") as Button; 
     if (_btnOk != null) 
     { 
      // do what you want with you button 
      _btnOk.Click += new RoutedEventHandler(_btnOk_Click); 
     } 
    } 

    void _btnOk_Click(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show("Ok button clicked"); 
    } 
} 

Generic.xaml(必須在(PROJECTDIR)/Themes/Generic.xaml) (不要忘記的xmlns:地方= 「CLR的命名空間:測試」)

<Style TargetType="local:ControlWithButtons"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:ControlWithButtons"> 
       <Border Background="Yellow" CornerRadius="5"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="30"/> 
         </Grid.RowDefinitions> 
         <Border Margin="10" Background="LightGray"> 
          <ContentPresenter/> 
         </Border> 
         <Button Grid.Row="1" x:Name="btnOk" Content="OK" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5"/> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

使用你的控制:

<Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
    <local:ControlWithButtons Width="300" Height="250" HorizontalAlignment="Center" VerticalAlignment="Center"> 

     <!-- TextBox is put into control --> 
     <TextBox Width="200" Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" /> 

     <!-- You can also specify ContentTemplate for ControlWithButtons --> 
     <!-- (see in MSDN "http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate(v=vs.95).aspx") --> 

    </local:ControlWithButtons> 
</Grid> 
+0

我找到了解決方案。 [ContentProperty( 「RootContentControl」)] – dermanyon

相關問題