question

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 asked EmonHaque-1485 commented

How to set value of Non-DependencyProperty of FrameworkElementFactory?

Here I'm adding Receivable objects in an ItemsControl with an ItemTemplate in Lease section:

93541-test.gif

The DataTemplate for the ItemTemplate is:

 DataTemplate receivableTemplate() {
     var grid = new FrameworkElementFactory(typeof(Grid));
     var col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
     var col2 = new FrameworkElementFactory(typeof(ColumnDefinition));
     var col3 = new FrameworkElementFactory(typeof(ColumnDefinition));
     var head = new FrameworkElementFactory(typeof(TextBlock));
     var amount = new FrameworkElementFactory(typeof(TextBlock));
     var button = new FrameworkElementFactory(typeof(CommandButton));
     col2.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto));
     col3.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto));
     amount.SetValue(Grid.ColumnProperty, 1);
     button.SetValue(Grid.ColumnProperty, 2);
     head.SetBinding(TextBlock.TextProperty, new Binding(nameof(Receivable.HeadId)) { Converter = new HeadIdToHeadNameConverter() });
     amount.SetBinding(TextBlock.TextProperty, new Binding(nameof(Receivable.Amount)));
     grid.AppendChild(col1);
     grid.AppendChild(col2);
     grid.AppendChild(col3);
     grid.AppendChild(head);
     grid.AppendChild(amount);
     grid.AppendChild(button);
     return new DataTemplate() { VisualTree = grid };
 }

so I've a CommandButton, button, in the third column but it doesn't appear because it needs some properties to be set. The plus button, I clicked to add those objects, is also a CommandButton and has been defined as:

 addReceivable = new CommandButton() {
     WidthAndHeight = 20,
     Icon = Icons.Plus,
     Command = viewModel.AddReceivable
 };

all these three properties are normal property. I want to set these values for the button in receivableTemplate function. How to do that?

windows-wpf
test.gif (173.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
1 Vote"
DaisyTian-1203 answered EmonHaque-1485 commented

From the Microsoft document FrameworkElementFactory.SetValue(DependencyProperty, Object) Method, use FrameworkElementFactory.SetValue can only set the value of a dependency property.
And in WPF Data binding, the target property must be a dependency property. If you want to bind data to some property, the property should be a dependency property. You could refer to Basic data binding concepts for more details.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DaisyTian-MSFT, looks like there's no alternative. I'd to create another generic CommandButton<T> and set those properties like this:

 button.SetValue(Grid.ColumnProperty, 2);
 button.SetValue(WidthProperty, 16d);
 button.SetValue(HeightProperty, 16d);
 button.SetValue(MarginProperty, new Thickness(10, 2, 0, 2));
 button.SetValue(CommandButton<Receivable>.IconProperty, Icons.Minus);           
 button.SetBinding(CommandButton<Receivable>.CommandProperty, new Binding(nameof(viewModel.RemoveReceivable)) { Source = viewModel });
 button.SetBinding(CommandButton<Receivable>.ParameterProperty, new Binding("."));

even though I'm not binding the IconProperty, I'd to declare it as a DependencyProperty

0 Votes 0 ·