WPF Binding a Dynamically Added Control - wpf

I am adding a to the "Company" RibbonApplicationMenuItem in my RibbonWindow with the following code:
var reset = DataContext as ICompanies;
if (reset != null)
{
// ToDo: Create interface to populate the mymenutems
var mymenuitems = new List<string>();
foreach (var item in mymenuitems)
{
var newbutton = new Button { Margin = new Thickness(2), Content = item };
MenuItem_Company.Items.Add(newbutton);
}
}
My XAML looks like this:
<ribbon:RibbonApplicationMenu ToolTipTitle="Application Menu">
<ribbon:RibbonApplicationMenuItem
Header="Company"
x:Name="MenuItem_Company"
ImageSource="Images\LargeIcon.png">
</ribbon:RibbonApplicationMenuItem>
</ribbon:RibbonApplicationMenu>
How do I bind my new button in code when I add it to the MenuItem_Company? I need it to bind to a property in my datacontext.
Thanks,
Eroc

var newbutton = new Button { Margin = new Thickness(2), Content = item };
Binding b = new Binding();
b.Source = reset;
b.Path = new PropertyPath("SomePropertyOnDataContext");
newButton.SetBinding(Button.IsEnabledProperty, b);
Varying assumptions in the code...but it should give you an idea where to start...

Related

Get the object created by new FrameworkElementFactory(typeof(T))

We are adding a DataGridTemplateColumn to a DataGrid, and populating the DGTC with a combobox created via
DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
FrameworkElementFactory comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
/* snip */
DataTemplate cellEditTemplate = new DataTemplate();
cellEditTemplate.VisualTree = comboBoxFactory;
dgtc.CellEditingTemplate = cellEditTemplate;
dgtc.SortMemberPath = string.Format($"{descr.BindingPropertyName}");
dgtc.Header = descr.BindingPropertyName;
Later, we need to change properties such as the TextSearch.TextPathProperty.
We get the CellEditingTemplate from the DataGridTemplateColumn; the question is, "How to access the CellEditingTemplate.VisualTree as a combobox"?
This code suggests that the VisualTree is a combobox:
var cellEditingTemplate = dgtc.CellEditingTemplate;
var propsVals = cellEditingTemplate.GetPropertyValues();
foreach (KeyValuePair<string, string> pair in propsVals)
{
Debug.WriteLine($"Key: {pair.Key} value: {pair.Value}");
}
Output:
Key: Type value: Windows.Controls.ComboBox
It appears there is no way to cast the VisualTree to ComboBox.
What am I overlooking?
Thanks --

Setting background property through Style in code does not work, but setting it directly does

I'm trying to set a style for a border's background color, the strange things is if I do it like this it works:
var borders = this.Editor.ChildrenOfType<Border>().Where(x => x.Name == "Background_Selected");
foreach (var border in borders)
{
border.Background = Brushes.Red;
}
But I want (need) to do it like this:
var borders = this.Editor.ChildrenOfType<Border>().Where(x => x.Name == "Background_Selected");
foreach (var border in borders)
{
var style = new Style(typeof(Border));
style.Setters.Add(new Setter()
{
Property = BackgroundProperty,
Value = Brushes.Red
});
border.Style = style;
}
What is going on here? why can't I set it with a style but can set it directly.
EDIT
The reason i need to do it like that is because I want to set this background color depending on a value of a bound object (which I was planning on setting like this):
var borders = this.Editor.ChildrenOfType<Border>().Where(x => x.Name == "Background_Selected");
foreach (var border in borders)
{
var binding = new Binding();
binding.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
binding.Path = new PropertyPath("Value.IsSelected");
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
var style = new Style(typeof(Border));
var trigger1 = new DataTrigger()
{
Binding = binding,
Value = true
};
trigger1.Setters.Add(new Setter()
{
Property = BackgroundProperty,
Value = Brushes.Green
});
var trigger2 = new DataTrigger()
{
Binding = binding,
Value = false
};
trigger2.Setters.Add(new Setter()
{
Property = BackgroundProperty,
Value = Brushes.Red
});
style.Triggers.Add(trigger1);
style.Triggers.Add(trigger2);
border.Style = style;
}

WPF prevent TreeViewItem to inherit contextMenu from parent by code

I have the following code and I set explicite the ContextMenu from a TreeViewItem to null but it shows all the time the same ContextMenu as the treeviewitem parents! Is that a bug? and how can I solve that?
Tree.Items.Clear();
System.Windows.Controls.TreeViewItem ceo = new System.Windows.Controls.TreeViewItem() { Header = "CEO" };
System.Windows.Controls.TreeViewItem manager1 = new System.Windows.Controls.TreeViewItem() { Header = "Manager1" };
System.Windows.Controls.TreeViewItem manager2 = new System.Windows.Controls.TreeViewItem() { Header = "Manager2" };
System.Windows.Controls.TreeViewItem person1 = new System.Windows.Controls.TreeViewItem() { Header = "person1" };
System.Windows.Controls.TreeViewItem person2 = new System.Windows.Controls.TreeViewItem() { Header = "person2" };
manager1.Items.Add(person1);
manager2.Items.Add(person2);
ceo.Items.Add(manager1);
ceo.Items.Add(manager2);
Tree.Items.Add(ceo);
// context Menu
MenuItem menu1 = new MenuItem();
menu1.Header = "Menu1";
MenuItem menu2 = new MenuItem();
menu2.Header = "Menu2";
MenuItem menu3 = new MenuItem();
menu3.Header = "Menu3";
MenuItem menu4 = new MenuItem();
menu4.Header = "Menu4";
MenuItem menu5 = new MenuItem();
menu5.Header = "Menu5";
MenuItem menu6 = new MenuItem();
menu6.Header = "Menu6";
System.Windows.Controls.ContextMenu ceoMenu = new ContextMenu();
ceoMenu.Items.Add(menu1);
ceoMenu.Items.Add(menu2);
ceoMenu.Items.Add(menu3);
System.Windows.Controls.ContextMenu managerMenu = new ContextMenu();
managerMenu.Items.Add(menu4);
managerMenu.Items.Add(menu5);
ceo.ContextMenu = ceoMenu;
manager1.ContextMenu = managerMenu;
manager2.ContextMenu = managerMenu;
System.Windows.Controls.ContextMenu personMenu = new ContextMenu();
personMenu.Items.Add(menu6);
person1.ContextMenu = personMenu;
person2.ContextMenu = null;
It works fine to set it like:
System.Windows.Controls.ContextMenu nullMenu = new ContextMenu();
nullMenu.Visibility = Visibility.Hidden;
person1.ContextMenu = personMenu;
person2.ContextMenu = nullMenu;
It's not a bug. If you set breakpoint on ContextMenuOpening event for person2, you will see, that it doesn't fired at all.
After that you can try to set breakpoint on ContextMenuOpening event for manager2. It's fired and on person2 node, but only because TreeViewItem is ItemsControl.
In WPF Visualizer we can see that:
It's not a bug. It is logical behavior. For example, if user clicks at the column at DataGrid, then it always shows the same ContextMenu. So if you set ContextMenu to null, then it just replaces your context menu by base context menu.
So we can make a little trick. We just change state of visibility of your ContextMenu.
So to have the personal Visibility of ContextMenu, you should just create another state of Visibility(Hidden, Visible or Collapse) to your created ContextMenu.
System.Windows.Controls.ContextMenu personMenu = new ContextMenu();
personMenu.Items.Add(menu6);
personMenu.Visibility = System.Windows.Visibility.Hidden;
person1TVI.ContextMenu = personMenu;
person2TVI.ContextMenu = personMenu;

Change Lineseries thickness in WPFToolkit.Chart C#

I'm trying change the line thickness in a serie dinamically created, I need turn the line more thick.
Below, follow the code to bind the created serie on chart component. It works fine, but I tryed adapt this in this code and I had no sucess.
Please help, thanks.
Style style = new Style(typeof(LineDataPoint));
style.Setters.Add(new Setter(LineDataPoint.OpacityProperty, (double)(0.0)));
style.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, dadosSerie.ColorSerie));
LineSeries lineSerie = new LineSeries()
{
Title = dadosSerie.SerieTitle,
IndependentValueBinding = new Binding("Key"),
DependentValueBinding = new Binding("Value"),
DependentRangeAxis = dadosSerie.EixoY,
DataPointStyle = style,
ItemsSource = dadosSerie.DataSerie,
};
chtGraficos.Series.Add(lineSerie);
Have you tried adding a Style for the serie's Polyline instead?
It seams the style for the LineDataPoint is actually for every point on the serie.
Here is a working sample of a chart fully created on code-behind. You just have to create a window named MainWindow and add a reference on the project to System.Windows.Controls.DataVisualization.Toolkit:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var valueList = new Dictionary<string, int>();
valueList.Add("Developer", 60);
valueList.Add("Misc", 20);
valueList.Add("Project Manager", 40);
var style = new Style(typeof(Polyline));
style.Setters.Add(new Setter(Polyline.StrokeThicknessProperty, 10d));
var series = new LineSeries
{
PolylineStyle = style,
ItemsSource = valueList,
DependentValuePath = "Value",
IndependentValuePath = "Key",
};
var lineChart = new Chart { Height = 254 };
lineChart.Series.Add(series);
var mainGrid = new Grid();
mainGrid.Children.Add(lineChart);
this.Content = mainGrid;
}
}

Combobox binding not working when window is in grid container

I try to bind a list in ComboBox with this code :
var template = new DataTemplate();
var comboBoxFactory = new FrameworkElementFactory(typeof(ComboBox));
template.VisualTree = comboBoxFactory;
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, this.GetType(), 1);
b.Path = new PropertyPath("DataContext");
comboBoxFactory.SetBinding(ComboBox.ItemsSourceProperty, b);
comboBoxFactory.SetBinding(ComboBox.SelectedItemProperty, new Binding("ClassRoom"));
It's working when I launch the window simply, like this :
winListOrgane ee = new winListOrgane();
ee.Show();
But when I try to put this window in a grid container, it's not working :
_currentWindow = (ModelPage)Activator.CreateInstance(pPage.GetType());
_currentWindow.SetParam(genParam);
_currentWindow.InitPage();
object content = _currentWindow.Content;
_currentWindow.Content = null;
_containerPage.Children.Add((UIElement)content);
What can I do to fix this problem?
Thanks
Best regards,
Frederic
It looks like it is your RelativeSource on the Binding. Since you are binding to a specific Window class - in this case it looks like GetType() will return typeof(winListOrgane). If you change that to typeof(Window), it should work:
b.RelativeSource =
new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1);

Resources