private void BindTextPath(TextBlock textBlock, string path, DeleagateBindingSource delBindSourse, object delBindSourcePar, string format) { try { Binding binding = new Binding(); binding.Path = new PropertyPath(path); binding.StringFormat = format; // "F0"; binding.Source = delBindSourse(delBindSourcePar); BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, binding); } catch (AggregateException e) { string aggr = ""; } catch (Exception e) { string st = ""; if (e != null) { Thread.Sleep(0); } } }
/// <summary> /// Universal method that creates one ControlTextBox. It could create static ControlHeaderBlock /// element that contains static text ("header").Or it could create text ControlTextblock element that /// bind with ViewModel elemnts. For create bindable ControlTextBlock it call specific parallel task. /// </summary> /// <param name="text">Static text for header or Path to ViewModel elemnts</param> /// <param name="row">Row in WPF Grid </param> /// <param name="col">Col in WPF Grid</param> /// <param name="colspan"></param> /// <param name="marginOffs"></param> /// <param name="controlTextType">Type (ControlTextBox or HeaderTextBox)depend on which create /// static element ControlHeaderBlock or binding from ViewModel ControlHeaderBlock /// </param> /// <param name="delToWait">Delegate for wait till it will be possible to bind</param> /// <param name="delToWaitPar">Parameter for delToWait</param> /// <param name="delBindSourse">Delegate retrieves Bind data source</param> /// <param name="delBindSourcePar">Parameter for delBindSourse</param> /// <param name="format">String format for output</param> private void CreateControlTextBlock(string text, int row, int col, int colspan, int marginOffs, enmControlTextTypes controlTextType, DelegateWait delToWait, object delToWaitPar, DeleagateBindingSource delBindSourse, object delBindSourcePar, string format) { TextBlock textBlock = null; System.Windows.Controls.UserControl controlText = null; if (controlTextType == enmControlTextTypes.ControlTextBox) { //TO DO make with interface controlText = new ControlTextBlock(); textBlock = ((ControlTextBlock)controlText).TextBlockLabel; } else if (controlTextType == enmControlTextTypes.HeaderTextBox) { controlText = new ControlHeaderBlock(); textBlock = ((ControlHeaderBlock)controlText).TextBlockLabel; } textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center; Thickness margin = controlText.Margin; margin.Left = marginOffs; margin.Right = marginOffs; Grid.SetRow(controlText, row); Grid.SetColumn(controlText, col); Grid.SetColumnSpan(controlText, colspan); GridInstrumentsData.Children.Add(controlText); if (controlTextType == enmControlTextTypes.ControlTextBox) { (new Task(() => TaskBindControlTextBlock(textBlock, text, delToWait, delToWaitPar, delBindSourse, delBindSourcePar, format))).Start(); } else if (controlTextType == enmControlTextTypes.HeaderTextBox) { textBlock.Text = text; } }
/// <summary> /// Wait till it it possible to bind (using specifig delegate) than call BindTextPath /// Call from CreateControlTextBlock /// </summary> /// <param name="textBlock"></param> /// <param name="path"></param> /// <param name="delToWait">Delegate for wait till it will be possible to binf</param> /// <param name="delToWaitPar">Parameter for delToWait</param> /// <param name="delBindSourse">Delegate retrieves Bind data source</param> /// <param name="delBindSourcePar">Parameter for delBindSourse</param> /// <param name="format">String format for output</param> private void TaskBindControlTextBlock(TextBlock textBlock, string path, DelegateWait delToWait, object delPar, DeleagateBindingSource delBindSource, object delBindSourcePar, string format) { try { while (delToWait(delPar)) { System.Threading.Thread.Sleep(1000); } GUIDispatcher.Invoke(new Action(() => BindTextPath(textBlock, path, delBindSource, delBindSourcePar, format))); } catch (Exception e) {// TODO throw exception ! string st = ""; if (e != null) { Thread.Sleep(0); } } }