English 中文(简体)
我可以创建一个具有“占位符”项的WPF自定义控件吗?
原标题:Can I create a WPF custom controll that has a "placeholder" item?

我们有很多控件,包括:一个带有圆形边框的容器和几个调用save&;取消视图模型上的命令,如下所示:

     <Border Background="White" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Opacity="1" Padding="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
        <StackPanel>
           <!--Some Control Stuff Here...-->

          <controls:SaveCancelButtons/>
        </StackPanel>
    </Border>

我想做的是制作一个自定义控件/style/template等,允许我重用它,这样我就可以将任何新的用户控件包装在一组标签中,将其内容放入堆栈面板(注释在上面)

实现这一目标的最佳方式是什么?

编辑:

好的,现在我有一个这样的模板:

<ControlTemplate x:Key="RoundedBordersTemplate">
    <Border Background="White" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Opacity="1" Padding="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
        <StackPanel>
            <ContentPresenter/>

            <controls:SaveCancelButtons/>
        </StackPanel>
    </Border>
</ControlTemplate>

控制是这样实现的:

<ContentControl Template="{StaticResource RoundedBordersTemplate}">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Description: " Width="72"/>
                    <TextBox Text="{Binding Path=Description}"
                        Width="205" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Type:" Width="72" />
                    <ComboBox ItemsSource="{Binding Path=TypeList}" 
                          DisplayMemberPath="Description" 
                          SelectedValuePath="ID"
                          Width="205" />
                </StackPanel>                
            </StackPanel>            
        </ContentControl>

但我只看到“保存/取消”按钮。

最佳回答

对于ContentControl类型,我建议使用ControlTemplate

<ControlTemplate x:Key="RoundedBordersTemplate" TargetType="ContentControl">
        <Border Background="White" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Opacity="1" Padding="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
            <StackPanel>
                <ContentPresenter />
                <controls:SaveCancelButtons/>
            </StackPanel>
        </Border>
    </ControlTemplate>

然后按如下方式使用:

<ContentControl Template="{StaticResource RoundedBordersTemplate}">
        <StackPanel>
            <Button>Hello</Button>
            <Button>World</Button>
        </StackPanel>
    </ContentControl>

给你:

更新:这样做的优点是不需要为仅视图更改子类ContentControl

问题回答

您的控件应继承自ContentControl(包含内容的控件,即其他控件)。像所有正确的WPF控件一样,这个控件是完全不可见的——其他东西(按钮的视觉外观)是通过模板/样式机制添加的。

然后,ContentControl的子类负责实际提供按钮的逻辑。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签