相关疑难解决方法(0)

WPF:带有2个(或更多!)ContentPresenters的模板或UserControl,用于在'slots'中显示内容

我正在开发LOB应用程序,我将需要多个对话框窗口(并且在一个窗口中显示所有内容不是一个选项/没有意义).

我希望我的窗口有一个用户控件来定义一些样式等,并且可以有几个插入内容的插槽 - 例如,模态对话框窗口的模板将有一个内容插槽和按钮(这样用户就可以提供带有绑定ICommands的内容和按钮组.

我想有这样的东西(但这不起作用):

UserControl xaml:

<UserControl x:Class="TkMVVMContainersSample.Services.Common.GUI.DialogControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
    >
    <DockPanel>
        <DockPanel 
            LastChildFill="False" 
            HorizontalAlignment="Stretch" 
            DockPanel.Dock="Bottom">
            <ContentPresenter ContentSource="{Binding Buttons}"/>
        </DockPanel>
        <Border 
            Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
            Padding="8"
            >
            <ContentPresenter ContentSource="{Binding Controls}"/>
        </Border>
    </DockPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?我怎么告诉VS我的控件暴露了两个内容占位符,以便我可以像这样使用它?

<Window ... DataContext="MyViewModel">

    <gui:DialogControl>
        <gui:DialogControl.Controls>
            <!-- My dialog content - grid with textboxes etc... 
            inherits the Window's DC - DialogControl just passes it through -->
        </gui:DialogControl.Controls>
        <gui:DialogControl.Buttons>
            <!-- My dialog's buttons with wiring, like 
            <Button Command="{Binding HelpCommand}">Help</Button>
            <Button Command="{Binding CancelCommand}">Cancel</Button>
            <Button …
Run Code Online (Sandbox Code Playgroud)

wpf user-controls contentpresenter

25
推荐指数
2
解决办法
3万
查看次数

命名空间中不存在新的WPF自定义控件库名称

我是WPF的新手,我正在尝试解决错误.我正在尝试构建一个自定义控件库,我可以在其中创建自己的控件对象.当我转到文件>新建项目> WPF自定义控件库> [输入名称]>保存,然后即时错误:

The name "CustomControl1" does not exist in the namespace "clr-namespace:ProjectName"
Run Code Online (Sandbox Code Playgroud)

我没有编辑任何代码,但立即出错.作为参考,错误在Generic.xaml中.

<ResourceDictionary
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:local="clr-namespace:ProjectName">
       <Style TargetType="{x:Type local:CustomControl1}">  //<--Fails here
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type local:CustomControl1}">  // Fails here as well
                       <Border Background="{TemplateBinding Background}"
                               BorderBrush="{TemplateBinding BorderBrush}"
                               BorderThickness="{TemplateBinding BorderThickness}">

                       </Border>
                   </ControlTemplate>
               </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

我正在使用Visual Studio 12和.NET 4.任何想法?

wpf intellisense visual-studio xaml-designer visual-studio-2012

11
推荐指数
2
解决办法
7282
查看次数

MyUserControl不能是XAML文件的根,因为它是使用XAML定义的

可能重复:
从WPF中的UserControl继承

我正在尝试创建一个WPF用户控件,它来自另一个用户控件,我也定义了它.

<myNamespace:NavigationControl
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:myNamespace="clr-namespace:myNamespace" mc:Ignorable="d"
x:Class="myNamespace.WelcomeScreen"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">
Run Code Online (Sandbox Code Playgroud)

这是WelcomeScreen.cs的样子:

public partial class WelcomeScreen : NavigationControl
{
    public WelcomeScreen()
    {
        this.InitializeComponent();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到以下错误:

'myNamespace.NavigationControl'不能是XAML文件的根,因为它是使用XAML定义的.

我究竟做错了什么?

c# wpf xaml

9
推荐指数
2
解决办法
1万
查看次数

在WPF中公开绑定的子控件依赖属性

我用这个XAML UserControl调用CustomTextBox了一个非常简单的东西:

<UserControl x:Class="CustomTextBox" ... >
    <Grid>
        <TextBox x:Name="InnerTextBox"/>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

现在当我使用CustomTextBox并想要绑定时InnerTextBox.Text,它不起作用:

... {Binding ElementName=CustomTextBox, Path=InnerTextBox.Text}
Run Code Online (Sandbox Code Playgroud)

我尝试了另一种方式,也不起作用:

... {Binding ElementName=CustomTextBox.InnerTextBox, Path=Text}
Run Code Online (Sandbox Code Playgroud)

我知道我可以定义一个新的依赖属性调用CustomTextBox.Text然后将其绑定到InnerTextBox.Text但我计划拥有许多属性的自定义控件,并且复制所有这些属性仅仅是为了支持绑定是一项工作.此外,复制/包装属性意味着每个值存储两次.

在WinForms中,这是一个简单的继承问题,所有属性都可以自动使用.在WPF中,无法继承XAML控件,并且无法访问属性.

有没有简单的方法来设置从某个控件到UserControl子元素属性的绑定?

.net wpf xaml dependency-properties custom-controls

5
推荐指数
1
解决办法
4679
查看次数

XAML 继承(非代码隐藏)

XAML 中的继承是如何实现的?

是否只能继承代码隐藏而不是.xaml相关问题)?将父控件包含在子控件的命名空间中是唯一的方法吗?

XAML本身似乎没有一般的“继承” 。有关于继承等的问题UserControl,但不是一般继承

问题:

我有 2 个 xaml 文件File1.xamlFile2.xaml,它们非常相似。我可以创建一个SuperFile.xaml并将以下大部分代码放入其中吗?

<UserControl ... >
    <Grid>
        <Grid.RowDefinitions ... />

        <DockPanel ... >
            <ToolBar ... >
                <!-- Some Buttons here:
                     File1.xaml to contain the Buttons "View" and "Export"
                     File2.xaml to contain the Buttons "Edit" and "Delete"
                 -->
            </ToolBar>
        </DockPanel>

        <igDP:XamDataGrid MouseDoubleClick="xamDataGrid1_MouseDoubleClick" ... />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

那唯一的东西不同,File1.xamlFile2.xaml有: …

.net c# wpf inheritance xaml

4
推荐指数
1
解决办法
1461
查看次数