[英]wpf - can't use custom control
I have a custom control inside a StackPanel 我在StackPanel中有一个自定义控件
<Window x:Class="Video_Editor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:m="clr-namespace:Video_Editor">
<Grid>
</StackPanel>
<ScrollViewer Margin="10,40,10,10" Grid.Row="2" VerticalScrollBarVisibility="Auto">
<StackPanel Name="stackPanel" >
<m:CustomControl Name="testControl"/>
</StackPanel>
</ScrollViewer>
</Grid>
The custom control currently doesn't do anything. 自定义控件当前不执行任何操作。
public class CustomControl: ItemsControl
{
}
I tried to do this in the window's constructor: 我试图在窗口的构造函数中执行此操作:
public MainWindow()
{
InitializeComponent();
testControl.Items.Add("item");
}
I am getting an error "the name "testControl does not exist in the current context. 我收到一个错误“名称”testControl在当前上下文中不存在。
You will have to use x:Name
since you are deriving from another FrameworkElement
that is exposing the Name
property itself. 您将不得不使用
x:Name
因为您从另一个暴露Name
属性本身的FrameworkElement
派生。
If a FrameworkElement
has set its Name
property (which ItemsControl
seems to be doing) you cannot declare the Name
property on the derived type, but you can use the Xaml
x:Name
property so you can decare a Name
and access from code behind 如果
FrameworkElement
已设置其Name
属性( ItemsControl
似乎正在执行),则无法在派生类型上声明Name
属性,但您可以使用Xaml
x:Name
属性,以便您可以从后面的代码中取消Name
和访问权限
Example: 例:
<StackPanel Name="stackPanel" >
<m:CustomControl x:Name="testControl"/>
</StackPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.