Access XAML object from code - wpf

I created a UserControl in WPF:
In Xaml:
<UserControl x:Class="OutlookPanel.MailRelation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
xmlns:graph="clr-namespace:MyPanel"
>
<DockPanel>
<graph:Graph Name="theGraph" NodesBindingPath="ChildNodes"
NodeTemplateSelector="{StaticResource nodeTemplateSelector}">
..
</DockPanel>
</UserControl>
I cs:
object theThing = e.Parameter;
((MailRelation)sender).theGraph.CenterObject = theThing;
This last sentence does not work as theGraph is not accessible.
Any idea why i can access theGraph ?
Thanks
John

Name="theGraph"
should be
x:Name="theGraph"
from http://msdn.microsoft.com/en-us/library/ms752059.aspx
x:Name: Specifies a run-time object
name for the instance that exists in
run-time code after an object element
is processed. You use x:Name for cases
of naming elements where the
equivalent WPF framework-level Name
property is not supported. This
happens in certain animation
scenarios.

Probably because that property is private. Provide a public getter and you should be able to get it. In your code, add something like
public Graph TheGraph { get { return theGraph; } }

Related

How to add UserControl to a Panel on a WPF Window

I think I'm missing something that should be obvious here, but I'm drawing a blank on this one.
I've built a very primitive UserControl containing nothing more than a TextBox to use as a log window:
<UserControl x:Class="My.LoggerControl"
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"
x:Name="LoggerView">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="LogWindow" AcceptsReturn="True"/>
</Grid>
</UserControl>
I don't expect that to be the best way to do it, but it should be good enough for a prototype.
The code-behind is similarly simple:
public partial class LoggerControl : UserControl, ILogger
{
public LoggerControl()
{
InitializeComponent();
}
private LogLevel level = LogLevel.Warning;
#region ILogger
public LogLevel Level
{
get { return level; }
set { level = value; }
}
public void OnError(string s)
{
if (level >= LogLevel.Error)
LogWindow.AppendText("ERROR:::" + s + "\n");
}
// ...
#endregion
}
The thing I can't figure out is how to add this control to my MainWindow.xaml. Simplifying, lets say my window looks like this:
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My"
Title="Test" Height="350" Width="525">
<Grid>
<local:LoggerControl x:Name="LogView" />
</Grid>
</Window>
Even with something so simple, the Designer in Visual Studio 2010 can't load the main window. The error given is:
A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.
This error message has only one unrelated hit in the major search engines (plus duplicates) so I haven't found any useful help. Microsoft's own documentation seems to imply that this should work.
Any idea how to solve this?
<UserControl x:Class="My.LoggerControl"
xmlns:local="clr-namespace:My.LogTest"
Looks like you may have made a mistake in the namespacing? LoggerControl is listed as being the namespace My, while you're importing My.LogTest and assigning it to the xml-prefix local. Change this to:
xmlns:local="clr-namespace:My"
And I think it should work. Otherwise, fix the LoggerControl declaration.

ObservableCollection doesn't get bound to ListBox in codebehind

I would like to bind a list box to an observable collection in code behind. This is what I am using for the binding:
Binding binding = new Binding();
binding.Source = symTable;
substanceList.SetBinding(ListBox.ItemsSourceProperty, binding);
symTable inherits from ObservableCollection, the Count property gets updated appropriatelly so I know I am adding elements correctly, but the list box isn't. I didn't know what to set the path to, since in XAML it is bound to the whole list.
Note: When adding individual items to the ListBox they get shown, so it is not a display issue. I also tried:
this.Resources.Add("symTable", symTable);
in the window constructor and then this:
but it says the resource cannot be resolved.
I also tried adding it as a resource in XAML but it didn't work again:
//in the window's resources.
<s:SymbolTable x:Key="symTable"/>
...
<ListBox x:Name="substanceList" ItemsSource="{Binding Source={StaticResource symTable}}"/>
and then in code behind:
symTable = (SymbolTable)this.FindResource("symTable");
Does anyone know any other way to do this in code behind or XAML, I think the ElementName refers to objects defined in code behind.
Here is part of the class definition for symTable:
public class SymbolTable : ObservableCollection<Substance>
{
Dictionary<string, Substance> symbolTable;
...
public Substance Insert(Substance s)
{
if (!symbolTable.ContainsKey(s.Name))
{
symbolTable.Add(s.Name, s);
Items.Add(s);
}
return symbolTable[s.Name];
}
Note alright so I just noticed the most weird thing, Items.Add wasn't raising the INotifyChanged event. I used Items.Add in my Insert method, I am guessing that Items.Add doesn't raise a INotifyChanged event so the ListBox wasn't getting updated, but when did Add instead of Items.Add then it worked. Do you know if this is indeed the case?
Ok, following the comments on your question, here is how to declare SymbolTable as resource in XAML and how to bind a ListBox to it. Note the XAML namespace declaration 'local', which refers to the local namespace/assembly, which is named ListBindingTest in my test project.
<Window x:Class="ListBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListBindingTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:SymbolTable x:Key="symTable"/>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource symTable}}"/>
</Grid>
</Window>
You may access the SymbolTable resource in code behind in your window class like this:
SymbolTable st = (SymbolTable)Resources["symTable"];
st.Add(new Substance());
The DisplayMemberPath needs to be a public property of symTable.
You could always just set the ItemSource directly on the listbox.
substanceList.ItemsSource = symTable;

"The attachable property not found in type " error while using a dependency property in Silverlight

I am trying to do some sample applications to use Dependency Property in a DataGrid,but when i tried to run application I am getting an run time exception
The attachable property 'SelectedColumnIndex' was not found in type
'CustomDependencyProperty'. [Line: 17 Position: 74]
This is the code i used to declare my dependency property
public class CustomDependencyProperty : DataGrid
{
public static DependencyProperty SelectedColumnIndexProperty = DependencyProperty.Register("SelectedColumnIndex",
typeof(object),
typeof(DataGrid),
new PropertyMetadata(0));
public int SelectedColumnIndex
{
get
{
return (int)GetValue(SelectedColumnIndexProperty);
}
set
{
SetValue(SelectedColumnIndexProperty, value);
}
}
}
And this is my XAML code
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="BindingDictionary.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindingDictionary"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<local:SimpleConverter x:Key="myConverter"></local:SimpleConverter>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid x:Name="dataGrid"
AutoGenerateColumns="True"
ItemsSource="{Binding Responses}"
local:CustomDependencyProperty.SelectedColumnIndex="{Binding Index,Mode=TwoWay}">
</sdk:DataGrid>
<TextBlock x:Name="DisplayIndex" Text="{Binding Index}" />
</Grid>
</UserControl>
I am unable to figure out what excatly is the problem.Is there anything wrong in the way I declare a dependency property?
Please help.
Thanks,
Alex
I think you need an attached property here. Try changing
DependencyProperty.Register
to
DependencyProperty.RegisterAttached.
Also, typeof(object) should be typeof(int).
UPDATE
Yes, the above will fix your problem, but I think you don't really need an attached property here as your class is extending the DataGrid class. A normal dependency property is all you need. So keep your existing code and change
typeof(object),typeof(DataGrid),
to
typeof(int),typeof(CustomDependencyProperty),
and in your xaml, you can just use this extended class directly, something like this,
<local:CustomDependencyProperty SelectedColumnIndex="{Binding Index,Mode=TwoWay}">
You might want to change the name 'CustomDependencyProperty' to be something more meanful like ExtendedDataGrid.
So I think the conclusion is you normally have two ways of creating a bindable property, either by extending the control and creating a normal dependency property, or by creating a static class with an attached property.
Hope this helps. :)
I think I can answer this question now.This exception just explains what exactly is the diffrence between an AttachedProperty and a DependencyProperty.
To use a dependency property SelectedColumnIndex I should redefine my DataGrid xaml like this
<local:CustomDependencyProperty x:Name="customGrid"
AutoGenerateColumns="True"
ItemsSource="{Binding Responses}"
SelectedColumnIndex="{Binding Index, Mode=TwoWay}">
</local:CustomDependencyProperty>

How do I bind a "list" of strings to a ComboBox in WPF?

I basically want to take a bunch of names in a collection and bind them to a combobox. For example:
Bill
Jack
Bob
Kevin
and have those items in a collection and have it bound to the ComboBox. I'm not sure if the list will be updated dynamically or not, but I prefer to plan for it to be. Any help would be appreciated. I've been trying for a few hours now and can't figure it out. I want to do it in XAML and not the code-behind. In the code-behind,
MyComboBox.ItemsSource = MyObservableCollection;
works fine. I don't know how to do that in XAML though with the collection declared in the code-behind.
Thanks in advance (again), community.
*EDIT:
This is how I have the collection declared and accessible.
public ObservableCollection<string> propertynames
{
get {return _propertynames;}
}
private ObservableCollection<string> _propertynames;
The last thing I tried was this:
<Window.Resources>
<CollectionViewSource Source="{Binding propertynames}" x:Key="srcSort"/>
</Window.Resources>
....
<ComboBox x:Name="cboSort" HorizontalAlignment="Left" VerticalAlignment="Top"
Width="256" Background="WhiteSmoke" Margin="12,50,0,0" FontSize="12pt"
Height="27.28"
SelectedIndex="0"
SelectionChanged="cboWorkCenters_SelectionChanged"
ItemsSource="{Binding Path = {StaticResource srcSort}}">
</ComboBox>
....
I'm a total n00b to this stuff. Been in it about a week now, so I may have done something really obvious to a seasoned user.
*EDIT #2
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:WpfApplication1"
Title="Window1" Height="226" Width="242"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<ComboBox Margin="43,71,40,77"
Name="comboBox1"
ItemsSource="{Binding ob}" />
</Grid>
</Window>
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public ObservableCollection<string> ob
{
get
{
return _ob;
}
}
private ObservableCollection<string> _ob = new ObservableCollection<string>();
public Window1()
{
InitializeComponent();
FillObj();
//comboBox1.ItemsSource = ob;
}
private void FillObj()
{
for (int i = 1; i < 6; i++)
{
_ob.Add(i.ToString());
}
}
}
}
Made above real simple project just to see if I was doing it all wrong. This worked fine so something else must be causing it to fail.
*EDIT #3
*PROBLEM FIXED
For God's sake, I figured it out. I've been on this for HOURS and it's just silly what's caused it to fail.
The solution is this: I wasn't instantiating _propertynames when I declared it. I was querying the class properties with Linq to get the list of properties and then created _propertynames by passing ...GetProperties.ToList<...>() to the constructor. Apparently, you have to instantiate the variable so it hits during InitializeComponent. Unreal.
Once I did that and then added the items to it after the fact, it worked fine.
I wish WPF had a face so I could punch it. I know it's my ignorance of how it works, but I really could have used some kind of message.
Thanks guys for the help. Both of your suggestions were useful once I took care of the root issue.
private ObservableCollection<string> _propertynames
needs to be
private ObservableCollection<string> _propertynames = new ObservableCollection<string>()
There are countless ways of doing this. Once you've created the collection in code-behind, you can:
Call Resources.Add to add it to the window's resource dictionary, and then bind to the resource, e.g. ItemsSource="{Binding {DynamicResource MyList}}".
Give the ComboBox a name (using the x:Name attribute) and set its ItemsSource explicitly in code, e.g. MyComboBox.ItemsSource = myCollection;.
Create a class, make the collection a property of the class, and set the window's DataContext to an instance of that class and bind to it directly, e.g. ItemsSource = "{Binding MyCollectionProperty}".
Make the collection a property of the window, set the window's DataContext to this, and bind to the property (this is essentially the same technique as #3, only you're not creating a new class).
Without setting the window's DataContext, you can still reference a property on it using binding as long as you've given it a name, e.g. {Binding ElementName=MyWindow, Path=MyCollection}. (This is the same as Ross's suggestion.)
Or, without giving the window a name, you can use RelativeSource binding to find the ancestor Window and bind to a property on it. I don't have any confidence in my ability to write a working binding expression that uses RelativeSource off the top of my head, so I'll leave that as an exercise for the reader.
You can set the DataContext of the ComboBox to the instance of your collection, and then set itsItemsSource to {Binding}. You probably wouldn't do this in practice; I mention it just because it seems to be a common mistake for people to set the DataContext of a control without also setting a binding, and then wonder why content from the bound object isn't showing up.
(While I've said "window" in the above, everything I've said is also true for user controls.)
I'm sure there are at least five other ways to do this that I'm not thinking of. Binding is really, really flexible.
What have you tried so far?
I would approach it as follows, assuming the combo box is within a UserControl with a code-behind class containing the public property MyObservableCollection:
<UserControl x:Name="MyCollectionOwnerControl">
<ComboBox ItemsSource="{Binding ElementName=MyCollectionOwnerControl, Path=MyObservableCollection, Mode=OneWay}" />
</UserControl>

DependencyProperty: VS cannot find the source for binding

I'm trying to bind the Forderground dependency property to my UIControl, so that it's drawn in the color the user wishes. Since myUiControl.Foderground autocopletes, I thought I could just bind it in the XAML file like this:
{Binding ElementName=rootControl, Path=Forderground}
When debugging VS says it cannot find the source for binding with this DependencyProperty.. but I couldn't figure out why this is.
Also how can I list all dependency properties of an object while debugging?
UPDATE: If below wasn't enough for you, try downloading this sample and looking at it.
The ElementName needs to be set as the "x:Name" of your root control and the Path needs to be set to the Property on the root element you wish to bind to. Without the name it cannot find the element you are referring to (hence the initial error) and without the Path it doesn't bind to the correct property (check your output at runtime for an error).
Try this:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid x:Name="root" Background="Green">
<Button Background="White" Margin="100">
<TextBlock Background="{Binding ElementName=root, Path=Background}" Text="TESTING TESTING"/>
</Button>
</Grid>
Can you confirm that your "rootControl" element is defined earlier in the xaml markup than your Binding holder? Usually the Bindings are bound to the earlier declared elements.
If you mean ImmediateWindow and IntelliSense usage while debugging than each dependency property metadata has usually public static access modifiers. You can for instance type "Control." and observe all the corresponding dependency properties, routed events and attached properties members.
Hope this helps.

Resources