简体   繁体   English

无法在“隐藏代码”中访问WPF用户控件

[英]Can't access WPF user control in Code Behind

I am using the following markup to define my controls. 我正在使用以下标记来定义我的控件。 I have seen various other examples of the point of declaring the x:Name attribute which I have done, but I still cannot select my control in the code behind. 我已经看到了声明x:Name属性的其他示例,但是仍然无法在后面的代码中选择控件。

<UserControl x:Class="AMTR2.UI.Controls.CustomColumnsBuilderControl"
             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="332" d:DesignWidth="1038">


    <Canvas>
        <ListView Canvas.Left="12" Canvas.Top="48" Height="183" Name="listView1" 
            Width="453" 
            SelectionMode="Single">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Order" Width="100" 
                        DisplayMemberBinding="{Binding Col1}"></GridViewColumn>
                    <GridViewColumn Header="Display Name" Width="290" 
                        DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
                    <GridViewColumn Header="Width" Width="50" 
                        DisplayMemberBinding="{Binding Col2}"></GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>
        <Button Name="moveUpButton" Canvas.Left="472.4" Canvas.Top="50" Content="Move Up" 
            Height="22" Width="74" IsEnabled="False"  />

        <Button Name="moveDownButton" Canvas.Left="472.4" Canvas.Top="80" Content="Move Down" 
            Height="22" Width="74" IsEnabled="False"  />


        <Button Name="deleteButton" Canvas.Left="472.4" Click="removeButton_Click" Canvas.Top="110" Content="Delete" 
            Height="22" Width="74"  />

        <Button Name="addButton" Click="addColumn" Canvas.Left="472.4" Canvas.Top="140" Content="Add Item" 
            Height="22" Width="74" />

        <Label Content="Name:" Canvas.Left="12" Canvas.Top="250" />

        <TextBox x:Name="txtDsiplayName"  Canvas.Left="12" Canvas.Top="280" Height="23"   
            Width="450.4"  />

        <Label Content="Width:"   Canvas.Left="470" Canvas.Top="250" />
        <TextBox x:Name="txtWdith" Canvas.Left="470"  Canvas.Top="280" Height="23"  
            Width="70.4" 
             />

    </Canvas>
</UserControl>

I am trying to access the control using the following routine in my code behind. 我试图在后面的代码中使用以下例程访问该控件。 I am coming from a winforms background so excuse me if this is not the correct way to go about it. 我来自winforms背景,所以请问这不是正确的解决方法。

private void addColumn(object sender, RoutedEventArgs e)

    {

        _ColumnsModeList.Add(new CustomColumnsModel
        {
            DisplayNameOverride =txtDsiplayName.Text,
            Width =100,
            Order=1
        });
    }

you can access any control by his name, for exemple: 您可以按他的名字访问任何控件,例如:

<UserControl x:Class="AMTR2.UI.Controls.CustomColumnsBuilderControl"
         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="332" d:DesignWidth="1038">


<Canvas>
    <ListView Name="listView" Canvas.Left="12" Canvas.Top="48" Height="183" Name="listView1" 
        Width="453" 
        SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Order" Width="100" 
                    DisplayMemberBinding="{Binding Col1}"></GridViewColumn>
                <GridViewColumn Header="Display Name" Width="290" 
                    DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
                <GridViewColumn Header="Width" Width="50" 
                    DisplayMemberBinding="{Binding Col2}"></GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>
    <Button Name="moveUpButton" Canvas.Left="472.4" Canvas.Top="50" Content="Move Up" 
        Height="22" Width="74" IsEnabled="False"  />

    <Button Name="moveDownButton" Canvas.Left="472.4" Canvas.Top="80" Content="Move Down" 
        Height="22" Width="74" IsEnabled="False"  />


    <Button Name="deleteButton" Canvas.Left="472.4" Click="removeButton_Click" Canvas.Top="110" Content="Delete" 
        Height="22" Width="74"  />

    <Button Name="addButton" Click="addColumn" Canvas.Left="472.4" Canvas.Top="140" Content="Add Item" 
        Height="22" Width="74" />

    <Label Content="Name:" Canvas.Left="12" Canvas.Top="250" />

    <TextBox x:Name="txtDsiplayName"  Canvas.Left="12" Canvas.Top="280" Height="23"   
        Width="450.4"  />

    <Label Content="Width:"   Canvas.Left="470" Canvas.Top="250" />
    <TextBox x:Name="txtWdith" Canvas.Left="470"  Canvas.Top="280" Height="23"  
        Width="70.4" 
         />

</Canvas>

in your code behind you indicate the name of the control, in this case listView . 在后面的代码中,指示控件的名称,在本例中为listView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法访问后面的代码中的控件ID - Can't access control ID in code behind 如何使用XAML和后面的代码序列化WPF用户控件 - How can I serialize wpf user control with xaml and code behind 从后面的代码添加WPF用户控件 - Adding a WPF User Control from Code Behind 使用WPF自定义控件,如何通过后面的代码为自定义控件提供名称以访问它? - Using a WPF Custom Control, How can I give my custom control a name to access it via the code behind? 如何从后面的代码刷新自定义WPF用户控件? - How can I refresh a custom wpf user control from code behind? 如何从背后的代码或通过绑定刷新WPF中的自定义用户控件 - How to refresh custom user control in WPF from code behind or by binding 在无法运行的代码中初始化用户控件-MVVM WPF - Initialising a user control in code behind not working - MVVM WPF 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? 无法通过代码访问Windows 8 Store用户控件 - Can't access Windows 8 Store user control from code 无法访问后面的代码“母版页”中的母版页控件ID - Can't access Master page control ID in code Master Page behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM