如何以编程方式将Javascript文件添加到用户控件?
我希望用户控件是一个完整的包 - 也就是说我不想在它使用的页面上添加与用户控件相关的javascript,当我可以在控件本身内完成时.
由于用户控件中没有Page对象,你会怎么做?
我正在创建一个UserControl,我只是记不起你用来装饰你想要作为默认内容属性的属性的属性的名称.
举一个具体的例子,假设我有一个名为'Title'的属性,我可以使用这样的属性语法设置 -
<local:myControl Title="the title"/>
Run Code Online (Sandbox Code Playgroud)
但是控件的使用者可能想要使用这样的元素语法 -
<local:myControl> the Title </local:myControl>
Run Code Online (Sandbox Code Playgroud)
我知道有一个属性我需要添加到Title属性以启用此支持,但我忘记了它是什么,无法在任何地方找到它.
有人能为我刷新记忆吗?此外,我正在寻找一个类似的属性来处理继承自ItemsControl的CustomControls.
我正在开发一个Windows窗体应用程序(.NET 2.0,VS 2005).我有一个表单,基本上包含一个动态调整大小的面板:
this.panel1.Dock=DockStyle.Fill;
Run Code Online (Sandbox Code Playgroud)
该面板仅用作容器.在运行时,将添加自定义控件:
UserControl uc=new UserControl();
panel1.Controls.Add(uc);
uc.Dock=DockStyle.Fill;
Run Code Online (Sandbox Code Playgroud)
由于此自定义控件具有最小大小要求,如果它太小而无法显示整个控件,我希望滚动条显示在包含的面板上:
this.panel1.AutoScroll=true;
Run Code Online (Sandbox Code Playgroud)
这不起作用.我试图使用Anchor属性而不是Dock属性来调整面板大小,但无济于事.
在Silverlight UserControls中实现自定义属性的正确方法是什么?
Silverlight中的每个"页面"在技术上都是UserControl(它们派生自UserControl类).当我在这里说UserControl时,我指的是一个自定义UserControl,它将在许多不同场景中的许多不同页面中使用(类似于ASP.NET UserControl).
我希望Custom UserControl支持Binding而不依赖于它绑定的Property的名称,始终保持相同.相反,我希望UserControl本身具有UserControl内的控件绑定的属性,并且UserControl外部的ViewModels也绑定到.(请看下面的例子)
UserControl中的绑定工作,MainPage中的绑定工作,我在MainPage和UserControl之间设置的绑定不起作用.特别是这一行:
<myUserControls:MyCustomUserControl x:Name="MyCustomControl2"
SelectedText="{Binding MainPageSelectedText, Mode=TwoWay}"
Width="200" Height="50" />
Run Code Online (Sandbox Code Playgroud)
示例输出:

MainPage.xaml中
<UserControl x:Class="SilverlightCustomUserControl.MainPage"
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:myUserControls="clr-namespace:SilverlightCustomUserControl"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Canvas x:Name="LayoutRoot">
<StackPanel Orientation="Vertical">
<TextBlock Text="UserControl Binding:" Width="200"></TextBlock>
<myUserControls:MyCustomUserControl x:Name="MyCustomControl2" SelectedText="{Binding MainPageSelectedText, Mode=TwoWay}" Width="200" Height="50" />
<TextBlock Text="MainPage Binding:" Width="200"></TextBlock>
<TextBox Text="{Binding MainPageSelectedText, Mode=TwoWay}" Width="200"></TextBox>
<Border BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding MainPageSelectedText}" Width="200" Height="24"></TextBlock>
</Border>
</StackPanel>
</Canvas>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
MainPage.xaml.cs中
namespace SilverlightCustomUserControl
{
public partial class MainPage : UserControl, INotifyPropertyChanged
{
//NOTE: …Run Code Online (Sandbox Code Playgroud) 标题几乎解释了这个问题.首次运行应用程序时,我将用户控件加载到主窗口中.我想要做的是在单击用户控件上的按钮时在父窗口上引发事件,那么如何从用户控件上的button1_Click引发父事件?
我正在使用以下按钮样式,它消除了边框并使透明背景形成我的图像按钮:
<Style x:Key="EmptyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#00000000"/>
<Setter Property="BorderBrush" Value="#00000000"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
样式来自以下答案: 带有圆形图像的WPF按钮
现在的问题是无论实际按钮有多大,可点击区域都被限制在图像中:

我的按钮代码:
<Button Name="n02" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Style="{DynamicResource EmptyButtonStyle}" Canvas.Left="308" Canvas.Top="157" Height="106" Width="120">
<Image Source="boto_face_off.PNG" Height="61" Width="59" />
</Button>
Run Code Online (Sandbox Code Playgroud)
问题:如何使整个按钮区域对点击作出反应?我想让它保持透明,没有现在的边框.
我在我的个人Libs中使用WPF UserControl.Libs包含在我的WPF和WindowsForms程序中.现在我的UserControl必须显示一个新的(WPF)窗口.在新窗口我想设置所有者.我是这样做的:
dialog.Owner = Application.Current.MainWindow;
Run Code Online (Sandbox Code Playgroud)
如果我在WPF程序中使用UserControl,这可以正常工作.
当我在我的WindowsForms程序中使用UserControl时(我在ElementHost中设置UserControl elementHost.Child = ...)为Application.Currentnull.
这不好,我的程序抛出异常.
为什么是Application.Currentnull?
我需要在页面上使用用户控件(.ascx),它是一个基于2个参数的相关用户控件:
1. Current post
2. Relation type
Run Code Online (Sandbox Code Playgroud)
页面需要具有此控件的3个不同实例,每个实例具有相同的当前post参数,但具有不同的关系类型(标题,作者,流派).
第一个参数我可以通过url得到它,但第二个参数怎么样?
我一直在谷歌搜索一段时间,但我还没有找到答案.如何传递第二个参数,以便控件可以根据这些参数加载信息?我宁愿不为每个参数创建一个控件,否则最好不构建用户控件但直接进入代码:(谢谢!
正如标记的答案所示,Reflector确认UserControl会覆盖一些方法,因此虽然两者之间的界面完全相同,并且您可以使用VS设计器,但行为上存在细微差别.我会让读者更多地研究这些差异,但这里是子类代码......
public class UserControl : ContentControl
{
// Fields
private static DependencyObjectType _dType;
// Methods
static UserControl()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(UserControl),
new FrameworkPropertyMetadata(typeof(UserControl)));
_dType = DependencyObjectType.FromSystemTypeInternal(
typeof(UserControl));
UIElement.FocusableProperty.OverrideMetadata(
typeof(UserControl),
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
KeyboardNavigation.IsTabStopProperty.OverrideMetadata(
typeof(UserControl),
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
Control.HorizontalContentAlignmentProperty.OverrideMetadata(
typeof(UserControl),
new FrameworkPropertyMetadata(HorizontalAlignment.Stretch));
Control.VerticalContentAlignmentProperty.OverrideMetadata(
typeof(UserControl),
new FrameworkPropertyMetadata(VerticalAlignment.Stretch));
}
internal override void AdjustBranchSource(RoutedEventArgs e)
{
e.Source = this;
}
protected override AutomationPeer OnCreateAutomationPeer()
{
return new UserControlAutomationPeer(this);
}
// Properties
internal override DependencyObjectType DTypeThemeStyleKey
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen …Run Code Online (Sandbox Code Playgroud) 我在usercontrol中创建了一个Dependency Property,但是usercontrol中的更改未通知Viewmodel
用户控件
<UserControl x:Class="DPsample.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox x:Name="txtName"></TextBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
UserControl.cs
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
#region SampleProperty
public static readonly DependencyProperty SamplePropertyProperty
= DependencyProperty.Register("SampleProperty",
typeof(string),
typeof(UserControl1),
new PropertyMetadata(OnSamplePropertyChanged));
public string SampleProperty
{
get { return (string)GetValue(SamplePropertyProperty); }
set { SetValue(SamplePropertyProperty, value); }
}
static void OnSamplePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
(obj as UserControl1).OnSamplePropertyChanged(e);
}
private void OnSamplePropertyChanged(DependencyPropertyChangedEventArgs …Run Code Online (Sandbox Code Playgroud) user-controls ×10
wpf ×5
.net ×2
asp.net ×2
winforms ×2
binding ×1
button ×1
c# ×1
elementhost ×1
events ×1
itemscontrol ×1
javascript ×1
scroll ×1
silverlight ×1
styles ×1
subclass ×1
windows ×1
wpf-controls ×1