Converter not working when DataTemplate is loaded in code behind - c#

I loaded the datatemplate for image column in code behind . Please refer the below code snippet,
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Image));
Binding bind = new Binding() { Path=new PropertyPath(imagecolumn.MappingName),Converter = new StringToImageConverter(),Mode=BindingMode.TwoWay };
fef.SetBinding(Image.SourceProperty,new Binding(imagecolumn.MappingName));
DataTemplate template = new DataTemplate() { VisualTree = fef };
this.imagecolumn.CellItemTemplate = template;
But my converter is not invoked. I need to load different images in each row of the column . Am i missed anything ? Please share any idea

You instantiate a new Binding but you never use it. Do this:
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Image));
Binding bind = new Binding() { Path=new PropertyPath("MappingName"),Converter = new StringToImageConverter(),Mode=BindingMode.TwoWay,Source=imagecolumn };
fef.SetBinding(Image.SourceProperty, bind); // here you just created
//another instance of Binding instead of using your bind variable
DataTemplate template = new DataTemplate() { VisualTree = fef };
this.imagecolumn.CellItemTemplate = template;
EDIT:
Have a look at FrameworkElementFactory. In the remarks it sais:
This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.
Maybe you should do it the recommended way.

Related

show values of mutiple properties in tool tip wpf XamDataGrid

I have xamdatgrid which I add columns dynamically. I am adding a ToolTip to the column using following code.
Binding toolTipBinding = new Binding();
toolTipBinding.Path = new PropertyPath("DataItem.Property1");
Setter toolTipSetter = new Setter();
toolTipSetter.Property = ToolTipProperty;
toolTipSetter.Value = toolTipBinding;
cellValuePresenterStyle.Setters.Add(toolTipSetter);
Using this I was able to display the value of Property1 as the ToolTip. I have another Property in the class (property2). I want to display the values of both Properties in the ToolTip. How can I achieve that
I can't understand why are you not doing it in XAML code. But if you do want to achieve this in using code behind. Then do following steps
Use MultiBinding class to do a binding of tooltip.
create two bindings for two properties (Property1 & Property2) and add them in multibinding you created in step 1 using Add method.
create a converter (IMultiValueConverter) to write a logic to concatinate property1 & property2.
Assign the binding in the setter value as you have already done.
Binding b1=new Binding();
b1.Path = new PropertyPath("DataItem.Property1");
Binding b2=new Binding();
b2.Path = new PropertyPath("DataItem.Property2");
var toolTipBinding = new MultiBinding();
toolTipBinding.Bindings.Add(b1);
toolTipBinding.Bindings.Add(b2);
toolTipBinding.Converter = new ///your converter

Setting the Datacontext of a dynamically created control

I have a UserControl (Map) that contains a Canvas control. I am dynamically adding a control (Gate) to this canvas from the code behind.
I want the Gate objects DataContext to be the "Gate" property of the Map's DataContext. This is being done in the code behind.
Binding dataContextBinding = new Binding();
dataContextBinding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
dataContextBinding.Path = new PropertyPath("DataContext.SelectedLevelModule.Gate");
dataContextBinding.Mode = BindingMode.TwoWay;
dataContextBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(gate, DataContextProperty, dataContextBinding);
After this block of code runs, the gate.DateContext is null...
Any ways this can be done? Drawing a blank..
Thanks
Harold
You are setting the property path to DataContext.SelectedLevelModule.Gate. You are then assigning the binding to the DataContextProperty. I think what is happening is that the path is now gate.DataContext.DataContext.SelectedLevelModule.Gate.
Try removing DataContext from your PropertyPath and see if that fixes it. You are already assigning it to the DataContext, you should not have to specify it in the Path.
var dataContextBinding = new Binding();
dataContextBinding.Path = new PropertyPath("SelectedLevelModule.Gate");
BindingOperations.SetBinding(gate, DataContextProperty, dataContextBinding);

Cell Style Binding does not Update on Value Change

I have a DataGrid which is bound to a Collection of Objects. Every DataGridColumn is created in code behind.
The Background of these columns is dependent on different values of the object. I create the background binding in the CellStyle (as it should not override default style's from triggers).
var backgroundBinding = new Binding
{
Converter = new MyBindingConverter(),
ConverterParameter = new MyConverterParameter()
};
cellStyle.Setters.Add(new Setter(Control.BackgroundProperty, backgroundBinding));
As you can see it binds directly to the element. As different value are changing the value of the Columns is updated accordingly, but the converter of the Binding is not called.
I tried calling OnPropertyChanged(null) to show that the object was updated, but sadly this does not work.
Did you try to specify Path for backgroundBinding? Something like:
var backgroundBinding = new Binding
{
Converter = new MyBindingConverter(),
ConverterParameter = new MyConverterParameter(),
ElementName = YourElementName,
Path = PropertyOnElement
};
cellStyle.Setters.Add(new Setter(Control.BackgroundProperty, backgroundBinding));

How to create a Grid and set RowDefinitations with FrameworkElementFactory in WPF?

I want to use FrameworkElementFactory to create a grid template, but how to define its RowDefinitions ?
as the code
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory RowDefinitionFactory1 = new FrameworkElementFactory(typeof(RowDefinition));
gridFactory.AppendChild(RowDefinitionFactory1);
FrameworkElementFactory RowDefinitionFactory2 = new FrameworkElementFactory(typeof(RowDefinition));
gridFactory.AppendChild(RowDefinitionFactory2);
it will throw exception...
because the <RowDefinitation> should be under <Grid.RowDefinitations>
FrameworkElementFactory has been deprecated:
This class is a deprecated way to programmatically create templates,
which are subclasses of FrameworkTemplate such as ControlTemplate or
DataTemplate; not all of the template functionality is available when
you create a template using this class. The recommended way to
programmatically create a template is to load XAML from a string or a
memory stream using the Load method of the XamlReader class.
The suggested route is to use XamlReader.Load or XamlReader.Parse if you need to programmatically create templates.
var template =
"<Grid>" +
"<Grid.RowDefinitions>" +
"<RowDefinition/>" +
"<RowDefinition/>" +
"</Grid.RowDefinitions>" +
"</Grid>";
var markup = XamlReader.Parse(template);

Get Binding from a FrameworkElementFactory

In a GirdView the text of some columns sould be aligned to the right.
To do that I create a DataTemplate, which contains a TextBlock.
Binding bd = new Binding("path");
FrameworkElementFactory tbContent = new FrameworkElementFactory(typeof(TextBlock));
tbContent.SetBinding(TextBlock.TextProperty, bd);
tbContent.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = tbContent;
myGridViewColumn.CellTemplate = dataTemplate;
In an other class I have to access the Bindings of my GridViewColumns. How can I access the Binding of this column?
I had the same issue, so I exposed the TextAlignmentProperty as a public property.

Categories