I have some controls where I set their Name
property to unique names, but I am unable to access them in the matching C# code file.
I have tried:
this.ControlName
MainWindow.ControlName
ControlName
but it does "see" them.
How do I do this?
Also do I have to do something special for nested controls inside wrap panels, grid views, etc?
EDIT:
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>
If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean. Save this answer.
Using a user control in XAML is pretty simple. I use a WPF Application to test the control. Create a WPF application project and copy the control code files to your project. After that, you need to add namespace of the library in which the user control is defined.
Controls in WPF are accessed by their Name properties. We specify the Name property in the XAML, and then can access the control by that name directly in C# code. Property notes. Name allows controls to interact.
For accessing any element in code behind you will need to set x:Name directive. It tells the XAML parser to add a field representing the named element to the automatically generated portion of the Window class just like Winforms.
In a WPF application, there’s no requirement to name each and every element. You should name only those elements which you want to programatically interact with.
An example:
<TextBlock x:Name="tblText" Text="Stackoverflow rocks."></TextBlock>
EDIT:
I used the following code and I was able to access the list view:
namespace WpfApplicationUnleashed
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
EffectsListView.Width = 10;
}
}
}
<Window x:Class="WpfApplicationUnleashed.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplicationUnleashed"
Title="Window1" >
<DockPanel>
<ListView x:Name="EffectsListView"></ListView>
</DockPanel>
</Window>
have you set their x:Name="ControlName"
property in xaml?
Here is more information on x:Name directive.
For example:
<Button x:Name="Button1">Click Me</Button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With