I have a UserControl that has a XamNumericEditor as a part of the Tree.
I am trying to set its binding value at runtime and add it dynamically to the children of a control.
This pattern works with TextBox, Button, ComboBox and some other controls. But I can't seem to get it to work with an Infragistic control. Nothing binds to it...but on all the other controls it works.
Please help!
Here is my simplified UserControl XAML
<UserControl x:Class="SilverlightApplication1.XText" 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" mc:Ignorable="d" xmlns:ig="http://schemas.infragistics.com/xaml" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <ig:XamNumericEditor Name="MyNumericEditor"/> </Grid></UserControl>Here is my code behindpublic partial class XText : UserControl { public XText() { InitializeComponent(); MyNumericEditor.SetBinding(XamNumericEditor.ValueProperty, new Binding() { Source = this, Path = new PropertyPath("MyText"), Mode = BindingMode.TwoWay }); } public double MyText { get { return (double)GetValue(MyTextProperty); } set { SetValue(MyTextProperty, value); } } public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register( "MyText", typeof(double), typeof(XText), new PropertyMetadata(null)); }And when I want to use it string s = @"<uc:XText MyText=""{Binding Path=" + path + @", Mode=TwoWay}""/>";
public partial class XText : UserControl { public XText() { InitializeComponent(); MyNumericEditor.SetBinding(XamNumericEditor.ValueProperty, new Binding() { Source = this, Path = new PropertyPath("MyText"), Mode = BindingMode.TwoWay }); } public double MyText { get { return (double)GetValue(MyTextProperty); } set { SetValue(MyTextProperty, value); } } public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register( "MyText", typeof(double), typeof(XText), new PropertyMetadata(null));
string s = @"<uc:XText MyText=""{Binding Path=" + path + @", Mode=TwoWay}""/>";
Anyone????? Ideas??? Please Help
Hello,
I'm not sure what exactly you're trying to do. I don't see any issue with your code.
What's the last line of your code supposed to do? It looks like you didn't paste the full sample. Are you trying to create a UserControl at runtime using XamlReader ?
Regards,
Hi,
You could check out the attached sample.
If it doesn't work for you, could you attach a small sample with your scenario ?
Thanks,
I see that your example works.
Here is a slimmed down version of mine.
The XamNumericEditor does not work, but if you comment out the XamNuemricEditor and uncomment the TextBox in both the markup and codebehind of XText, you will see that it works with the TextBox but not the XamNumericEditor.
The reason why I am wrapping this control, is so I can register events to it. Since I am using XamlReader, I cant register an event inside the Xaml that I am loading. So instead I load a User control that wraps the XamNumericEditor or TextBox and inside the wrapper contains the registered events.
Thanks!
It looks that this is caused by some timing issue. I moved the binding on the Loaded event of the control to make sure everything's initialized at that point and it worked fine.
However, this revealed another minor issue - the numbers being displayed were truncated, so I've set a MinWidth value of the NumericEditor in the DataTemplate.
I'm attaching a solution with the changes I've made.
I get an exception when I click into one numeric editor and then click into a different one. Then clicking into the original numeric editor is give me this exception...If that doesnt give you the exception just start click in different numeric editors it will eventually throw this:
'Operation is not valid due to the current state of the object'
Hi gmcalab,
Try to do this:
public XText()
{
InitializeComponent();
this.Dispatcher.BeginInvoke(() =>
this.MyNumericEditor.SetBinding(XamNumericEditor.ValueProperty,
new Binding()
Source = this,
Path = new PropertyPath("MyText"),
Mode = BindingMode.TwoWay
});
}
Well it will work if I don't use it for both the ItemTemplate and the EditorTemplate.
I guess I can work with that... unless there is another solution.
Is it necessary to use ‘XamNumericEditor’ for the ‘ItemTemplate’ ? You can use, in this case the ‘TextBlock’ and your code will look:
public MainPage()
int numOfColumns = 3;
for (int i = 0; i < 10; i++)
Row r = new Row();
for (int j = 0; j < numOfColumns; j++)
Cell c = new Cell();
c.Key = keys[j];
if( i > 0 )
c.Value = String.Format("{0}{1}", i, j);
else
c.Value = String.Format("{0}", j);
r.Cells.Add(c);
r.RowIndex = i;
Rows.Add(r);
for (int i = 0; i < numOfColumns; i++)
UnboundColumn column = new UnboundColumn();
column.ItemTemplate =
CreateItems(String.Format("RowData.Cells[{0}].Value", i));
column.EditorTemplate =
Create(String.Format("RowData.Cells[{0}].Value", i));
column.Key = keys[i];
XamGrid.Columns.Add(column);
this.model.Rows = this.Rows;
this.XamGrid.DataContext = this.model;
public static DataTemplate CreateItems(string path)
return (DataTemplate)XamlReader.Load(
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/client/2007""
xmlns:uc=""clr-namespace:TestApp;assembly=TestApp"">
<TextBlock Text=""{Binding Path=" + path + @", Mode=TwoWay}""
MinWidth=""100""/>
</DataTemplate>"
);
Will this help?