标签: user-controls

WPF用户控件的加载事件不止一次触发

要在WPF中实现基于选项卡的环境,我们需要将表单转换为用户控件,但是在执行此操作时,将Loaded调用用户控件的事件两次.

在互联网上搜索时,其他人也指出了这个问题.我们怎样才能确保只调用一次加载的事件?因为当它被多次调用时,我们的控件的初始化会多次发生.

c# wpf user-controls

34
推荐指数
2
解决办法
4万
查看次数

winforms控件的全局异常处理

在处理ASP.NET 1.1项目时,我总是使用Global.asax来捕获所有错误.我正在寻找一种类似的方法来捕获Windows窗体用户控件中的所有异常,最终成为托管的IE控件.做这样的事情的正确方法是什么?

error-handling user-controls winforms

33
推荐指数
3
解决办法
1万
查看次数

无法在表单上放置用户控件

我使用VS2010创建了一个C#WinForms应用程序.我是创建用户控件的新手,所以我创建了一个新的用户控件(作为同一项目的一部分).

重建项目时,新控件将显示在工具箱中.当我将控件从工具箱拖到窗体上时,我收到以下错误.

无法加载工具箱项"TagGroup".它将从工具箱中删除.

这是我创建用户控件的唯一时间.我在网上搜索过,但我发现的大多数答案似乎与将控件放在一个单独的程序集中有关.(请注意,我发现了很多与我遇到的问题相同的问题.)

任何人都可以建议我下一步要看哪里?

.net c# user-controls winforms

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

如何将UserControl中的控件设为私有?

我有一个ComboBox和TextBox的用户控件.一切都很好,除了我注意到从我的用户控件的实例对象,我可以访问这两个控件.除非通过我自己的暴露属性,否则不应该访问它们.

wpf user-controls access-modifiers

32
推荐指数
1
解决办法
4869
查看次数

WPF用户控件中的数据绑定

我正在为几个窗口共享的一系列控件创建一个UserControl.其中一个控件是Label,它根据"协议编号"显示其他一些过程的流程.

我正在尝试使用此Label提供DataBinding,因此Window会在协议编号变量更改时自动反映进程的状态.

这是用户控件XAML:

<UserControl Name="MainOptionsPanel"
    x:Class="ExperienceMainControls.MainControls"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    >
<Label Height="Auto" Name="numberLabel">Protocol:</Label>
<Label Content="{Binding Path=ProtocolNumber}" Name="protocolNumberLabel"/>
(...)
</UserControl>
Run Code Online (Sandbox Code Playgroud)

这就是Code-Behind:

public partial class MainControls 
{
    public MainControls()
    {
        InitializeComponent();
    }

    public int ProtocolNumber
    {
        get { return (int)GetValue(ProtocolNumberProperty); }
        set { SetValue(ProtocolNumberProperty, value); }
    }

    public static DependencyProperty ProtocolNumberProperty = 
       DependencyProperty.Register("ProtocolNumber", typeof(int), typeof(MainControls));
}
Run Code Online (Sandbox Code Playgroud)

这似乎是有效的,因为如果在构造函数中我将ProtocolNumber设置为任意值,它将反映在用户控件中.

但是,在最终窗口上使用此用户控件时,数据绑定会中断.

XAML:

<Window x:Class="UserControlTesting.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:expControl="clr-namespace:ExperienceMainControls;assembly=ExperienceMainControls"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    >
    <StackPanel>
        <expControl:MainControls ProtocolNumber="{Binding Path=Number, Mode=TwoWay}" />
    </StackPanel>

</Window>
Run Code Online (Sandbox Code Playgroud)

窗口的代码隐藏:

public partial class …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml user-controls

32
推荐指数
2
解决办法
8万
查看次数

Visual Studio设计器中的抽象UserControl继承

abstract class CustomControl : UserControl 
{
    protected abstract int DoStuff();
}

class DetailControl : CustomControl
{
    protected override int DoStuff()
    { 
        // do stuff
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在表单中删除了一个DetailControl.它在运行时正确呈现,但设计器显示错误并且无法打开,因为基本用户控件是抽象的.

目前,我正在考虑以下补丁,这对我来说似乎很不对,因为我希望子类被强制实现该方法.

class CustomControl : UserControl 
{
    protected virtual int DoStuff()
    {
        throw new InvalidOperationException("This method must be overriden.");
    }
}

class DetailControl : CustomControl
{
    protected override int DoStuff()
    { 
        // do stuff
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都有更好的想法如何解决这个问题?

c# user-controls abstract-class winforms

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

如何在WPF或WinForms中呈现公式

更新2018 TL; DR; LaTEX for WPF https://github.com/ForNeVeR/wpf-math

原始问题 我需要有一种方法在Windows窗体或WPF中绘制数学公式.一些"FormulaTextBox"控件正是我需要的.

我不是要问LaTEX的完整实现,但至少有一些超出RichTextBox的可能性,包括分割线,平方根等.

c# wpf user-controls winforms

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

奇怪错误 - CS0012:类型x在未引用的程序集中定义

类型"x"在未引用的程序集中定义.您必须添加对程序集"abc123"的引用.

我有一个.NET 2.0 Web应用程序,引用我的程序集'abc123'.该程序集存在于GAC中,我已经验证它是正确的(相同)版本.除了一个.aspx页面之外,其余的应用程序都没有问题.有问题的页面有一个转发器,它将用户控件显示为其"字段"之一.在将类型y的列表绑定到转发器时,我向用户控件传递类型x的列表(y的属性),如下所示:

<uc1:usercontrol id="ucusercontrol " runat="server" myPublicUserControlProperty='<%#Eval("CollectionOfX") %>'/>
Run Code Online (Sandbox Code Playgroud)

在用户控件的属性集上,我将类型x的列表绑定到用户控件中的gridview.

需要注意的一件奇怪的事情是,这个报告在我的开发PC上工作正常,但在我部署之后在任何服务器上都没有.我的电脑是Windows XP,IIS6,VS2005.服务器是Windows Server 2003,IIS6.

我希望我解释得那么好.提前感谢您提供的任何见解.

wpf user-controls assemblies repeater .net-2.0

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

WPF在单独的网格之间共享列宽

我的WPF UserControl上有以下设置:

<GroupBox>
  <Grid>
    ...
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />

<GroupBox>
  <Grid>
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="..." />
Run Code Online (Sandbox Code Playgroud)

我希望第二个ColumnDefinition与第一个ColumnDefinition的宽度相同,但我不想设置显式宽度.相反,我希望两个网格列自动拉伸到任一网格列中最长内容的宽度!

这可能吗?

wpf grid user-controls styling

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

从用户控件访问父控件 - C#

如何在C#(winform)中访问用户控件的父控件.我使用以下代码,但它不适用于所有类型的控件,如ListBox.

Control[] Co = this.TopLevelControl.Controls.Find("label7", true);
Co[0].Text = "HelloText"
Run Code Online (Sandbox Code Playgroud)

实际上,我必须从用户控件中添加放置在父"Form"上的Listbox中的项目.

c# user-controls winforms

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