WPF cannot access nested control - c#

Wondering if there's any trick to accessing a nested control in the code-behind ?
given some XAML along the lines of
<UserControl>
<textbox />
<DataGrid Name="MyGrid">
<Columns>
<Column field=ABC>
<EditType>
<ComboBox Name="myCombo1"/>
I can access this.MyGrid but cannot access this.myCombo1 !!

Everytime that I have run into this I have be able to resolve it by using x:Name instead of Name. This may not be the issue in your case but give it a try.

In simple cases you can access elements using their name, but looks like combobox is datatemplate part, you can work with visual tree at runtime using VisualTreeHelper, but if you use mvvm & code right usually you don't need to access elements directly. Can you provide more information on general problem or test project to reproduce issue?

Related

Creating xaml element in code-behind using gong dragdrop

I am fairly new to C#. This may be a fairly straight forward question, but I couldn't find any example about this. I am just wondering if there is any way when I create XAML element in the code-behind file(.xaml.cs), I can use other open source library(such as GongSolutions.WPF.DragDrop) property in the element?
An example will be as following, can I create the following XAML code in the code-behind(.xaml.cs) file?
<...
xmlns:dd="urn:gong-wpf-dragdrop"
...
>
...
<ListBox ItemsSource="{Binding Collection}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True" />
Thanks for your help!!!
You generally need to know how to set values for AttachedProperties from code behind. Below code should help for your case,
listBox.SetValue(GongSolutions.Wpf.DragDrop.DragDrop.IsDragSourceProperty, true);
listBox.SetValue(GongSolutions.Wpf.DragDrop.DragDrop.IsDropTargetProperty, true);
If you want to be type safe, you can set the same like this,
GongSolutions.Wpf.DragDrop.DragDrop.SetIsDragSource(listBox, true);
GongSolutions.Wpf.DragDrop.DragDrop.SetIsDropTarget(listBox, true);

How does WPF allow named items to be recognized across the entire application?

When a named XAML element is used in a WPF application, it can be accessed from anywhere. For example:
<Grid>
<Grid>
<TreeViewItem Name="itemScreen" />
The element itemScreen will be directly accessible in MainWindow(), although it is several levels deep in the XAML hierarchy.
How does WPF enable this to work in C#?
There's a mechanism called NameScope.
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/wpf-xaml-namescopes
Simple markup you put in a window which has no templating or styling will all have the one namescope.
If you dig through that link it will explain about styles and templates in more detail. Essentially, they have their own namescope.
This is probably as far as you want to go with an explanation at this stage but there are a couple of oddities like when you "inherit" a style using basedon.
I wouldn't worry about them just yet but throw it to the back of your mind for later.
ps
That control is a private member of your window and the name doesn't have to be unique across the entire application.

How to get an icon in stackPanel in a XAML file

I am new to XAML and C#
I have an icon created already in a project and and I have to use this icon whenever I select one of the option from the dropdown menu.
I made a stackpanel in XAML file
<StackPanel Name="stackPanelforIcon">
</StackPanel>
In the code behind file I have different cases for the dropdown menu.
case IconOnSelect:
?????? = IconList.NewIcon;
This NewIcon is the one already created and I am using the source also for this
using IconProject.Iconlists;
On writing IconList.NewIcon I am not getting any error, it is referenced correctly.
What should I write at ?????? to reference it. Is there any other way apart from using stackPanel to include an icon
A StackPanel cannot show an icon on it's own. You need a control for it, for example an Image.
<StackPanel Name="stackPanelforIcon">
<Image x:Name=theImage" />
</StackPanel>
Then you can use your Icon in your code behind like this:
this.theImage.Source = IconList.NewIcon;
You may need to convert your value, you never said what type it actually is.
Please note that using code-behind is not the preferred way with WPF. Using MVVM is way easier and more natural working with WPF, using code-behind you will fight WPF all the way. Using MVVM, this could be:
<StackPanel Name="stackPanelforIcon">
<Image Source="{Binding CurrentImage}" />
</StackPanel>
with your ViewModel having a property called CurrentImage that you would set when you want to change it. Don't forget to implement INotifyPropertyChanged for the changes to take effect though.

Overriding default template for specific nodes in a databound (XML/XPath) TreeView

TL;DR up front: I would like to use a "default" HierarchicalDataTemplate for all but a specific few nodes in a WPF TreeView. These nodes come from an XMLDocument and are not fully known until runtime. Is there a way to do this?
At runtime, I am analyzing specific parts of a system, and building an XML document that I'm then binding to a TreeView like so:
MyTree.DataContext = MyXMLDocument;
This is my WPF declaration of the TreeView:
<TreeView x:Name="MyTree" ItemsSource="{Binding Mode=OneWay, XPath=/Analysis}" ItemTemplate="{StaticResource GenericElementWithChildren}"/>
The template starts like this...
<HierarchicalDataTemplate x:Key="GenericElementWithChildren" ItemsSource="{Binding XPath=child::node()}">
So for example, I might have some long XML document about different aspects of the analysis I just ran and I'd like some particular element like "Disk" or "Proprietary Foo Service" to have a special template because it should be displayed nicely, while everything else just gets a generic template.
I've thought about using a DataTemplateSelector but it seems like there must be a better way. I'm not even sure how I'd do that for XML. Do any of you smarter folks have any wisdom to impart, or am I stuck figuring out how to write a DataTemplateSelector against XML?
DataTemplate and HierarchicalDataTemplate also have a DataType property.
When you remove the x:Key and supply a DataType, these Templates are implicit. So you can define your different templates as implicit and they will be used automatically, as long as you don't supply a ItemTemplate or ItemTemplateSelector on the inital TreeView.
<HierachicalDataTemplate DataType="{x:Type ProprietaryFooService}">
Personally i try to avoid that, because this also means that other controls that can show your data are using these Templates aswell. Imo the best would be to use a DataTemplateSelector, especially when you deal with the same types that you need to show in different ways.
Edit:
Sorry i missed that you are using Xpath. I guess it will not work there. I will leave this answer here, but can't guarantee that it suits your needs. Maybe it helps in another way anyway.

Binding a method to a column in datagrid WPF

I have a data grid in wpf which is bound to Collection. In one of the columns i want to bind a public method which returns string instead of a property.
Is there a way to resolve this in WPF.
By the way its one way binding.
I'm not entirely sure what you want to do and the advice of the previous two answers may be (and probably are more) appropriate in your scenario but just you answer your question, you can indirectly bind to a method using an ObjectDataProvider.
<Window>
<Window.Resources>
<ObjectDataProvider x:Key="newGuidProvider"
ObjectType="{x:Type Guid}"
MethodName="NewGuid"
/>
</Window.Resources>
...
<TextBlock Text="{Binding Source={StaticResource newGuidProvider}" ... />
...
</Window>
This is just a quick example and you can look into the ObjectDataProvider to see if it's right in your scenario. Here is a great resource which shows additional possibilities such as passing parameters to a method etc., via bindings.
You may be able to accomplish this by using
some evil tricks
an IValueConverter
an attached property
a behavior
by creating a read only proxy property.
However I'll would recommend using a property. It's the way WPF is supposed to work and handles all UI updating logic for you, too.
Why do you want to bind to a method?
If I right understand what you want, it should be enough to you to implement IValueConverter interface and assign it in XAML to you columns data binding's Converter attribute: here is an example how to use it: WPF Converter Example
for more detailed analysis can have a look on SvnRadar opensource project that use a bunch of them.
EDIT
There is no DataGrid control actually, there is a ListView, but the consept is the same.
Hope this helps.

Categories