当我创建WPF UserControl并尝试使用它时,我得到以下编译器错误:
由于'UserControl1'在同一程序集中实现,因此必须设置x:Name属性而不是Name属性.
关于如何解决它的消息很清楚,但它的原因是什么?为什么我不能Name在这里使用?
我在XAML中创建了一个控件,我希望通过Code Behind访问它.
<wincontrols:LiveTileFrameElement Name="PendingAuthsFrame1" Text="Pending" />
this.PendingAuthsFrame1.Text = "334";
Run Code Online (Sandbox Code Playgroud)
但是,我在上面的第二个语句中得到了一个构建错误,说明MyApp.MainWindow does not contain a definition for 'PendingAuthsFrame1' and no extension method....
它一直在继续,但你得到了我猜的想法.
我错过了什么或做错了什么?
AutomationProperties.Name和之间的"CodedUI测试构建器"没有区别x:Name.但第一个可以覆盖第二个.此外,AtomationProperties.Name支持数据绑定,x:Name当然不支持.
我们知道如果您使用MVVM模式,最好只x:Name在需要时使用.
所以应该AutomationProperties.Name优先考虑x:Name?
我有一些控件,我将其Name属性设置为唯一的名称,但我无法在匹配的C#代码文件中访问它们.
我试过了:
this.ControlName
MainWindow.ControlName
ControlName
Run Code Online (Sandbox Code Playgroud)
但它确实"看到"了他们.
我该怎么做呢?
我还需要为包装面板,网格视图等中的嵌套控件做些特别的事情吗?
编辑:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
namespace EditorWindow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow ( )
{
}
}
}
<Window x:Class="EditorWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Effects Editor">
<DockPanel>
<ListView x:Name="EffectsListView">
</ListView>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud) 我有以下Template为我Button
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<DockPanel Width="Auto">
<Button DockPanel.Dock="Top">
<Button.Template>
<ControlTemplate >
<Image Source="{Binding image}"/>
</ControlTemplate>
</Button.Template>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<local:GridLengthAnimation
Storyboard.TargetName="col1"
Storyboard.TargetProperty="Width"
LeftGridWidth="*" RightGridWidth="1*" Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</DockPanel>
</DataTemplate>
</Window.Resources>
<Grid>
...
...
<Grid Grid.Row="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition Name="col1" Width="{Binding ElementName=root, Path=DataContext.gla.LeftGridWidth}" />
<ColumnDefinition Name="col2" Width="{Binding ElementName=root, Path=DataContext.gla.RightGridWidth}" />
</Grid.ColumnDefinitions>
<Grid x:Name="LeftGrid" Grid.Row="2" Grid.Column="0" >
<Border BorderThickness="1" BorderBrush="Red">
<ItemsControl ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ElementName=root, Path=DataContext._movies}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border> …Run Code Online (Sandbox Code Playgroud) 例如,我有这个代码:
<Grid>
<Rectangle Name="TheRectangle" Fill="AliceBlue" Height="100" Width="100">
</Rectangle>
</Grid>
Run Code Online (Sandbox Code Playgroud)
VS.
<Grid>
<Rectangle x:Name="TheRectangle" Fill="AliceBlue" Height="100" Width="100">
</Rectangle>
</Grid>
Run Code Online (Sandbox Code Playgroud)
非常感谢你提供的信息.我很高兴能够学习这样的新东西.:d
我是Silverlight的新手.
当我使用Visual Studio向我的xaml文件添加一些控件时,它使用Name属性设置控件名称,但也有x:Name.
是否存在差异以及何时使用它们?
谢谢.
silverlight xaml silverlight-2.0 silverlight-3.0 silverlight-4.0
<Border Name="ItemBorder" Margin="5 5 0 5" BorderBrush="Black" BorderThickness="1" Height="75" Width="75">
<Border.Background>
<SolidColorBrush x:Name="ItemBorderBrush" Color="LightBlue"/>
</Border.Background>
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="someEvent">
<BeginStoryboard>
<Storyboard TargetName="ItemBorderBrush" TargetProperty="Color" Duration="0:0:1" >
<!--Storyboard TargetName="ItemBorder" TargetProperty="Background.Color" Duration="0:0:1"> -->
<ColorAnimation To="White"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
Run Code Online (Sandbox Code Playgroud)
我会试着清楚地解释我的问题.故事板目标名称,当它是"ItemBorder"(注释掉的行)时间歇地工作.有时我会收到一个错误,即在范围内找不到名称"ItemBorder".
我决定遵循MSDN示例中的样式,并直接在画笔上更改颜色属性,而不是将故事板的目标作为边框,并按属性更改边框画笔的颜色(注释掉的行).这似乎有效.
但是,Name="ItemBorderBrush"不编译因为Name不是属性SolidColorBrush所以我使用x:Name="ItemBorderBrush"Both Name并且x:Name被Border接受.为什么是这样?
什么x:意思(如何x:Name不同Name),以及为什么有Name边界的属性有时只与故事板一起工作?
我在我的数据网格中找到了Columns集合,并希望迭代它以找到某个列Name.但是,我无法弄清楚如何处理列的x:Name属性.这个xaml说明了我的DataGridTextColumn和DataGridTemplateColumn的问题:
<t:DataGrid x:Name="dgEmployees" ItemsSource="{Binding Employees}"
AutoGenerateColumns="false" Height="300" >
<t:DataGrid.Columns>
<t:DataGridTextColumn x:Name="FirstName" Header="FirstName"
Binding="{Binding FirstName}" />
<t:DataGridTemplateColumn x:Name="LastName" Header="LastName" >
<t:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</t:DataGridTemplateColumn.CellTemplate>
</t:DataGridTemplateColumn>
</t:DataGrid.Columns>
</t:DataGrid>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
DataGrid dg = this.dgEmployees;
foreach (var column in dg.Columns)
{
System.Console.WriteLine("name: " + (string)column.GetValue(NameProperty));
}
Run Code Online (Sandbox Code Playgroud)
在运行时,不存在任何值; column.GetValue不返回任何内容.使用Snoop,我确认DataGridTextColumn或DataGridTemplateColumn上没有Name属性.
我错过了什么?