Similar questions were asked many times, but I cannot quite make them work. How would I create C# version of the following xalm statement:
<h:TubeVisual3D x:Name="PipeVisual" Path="{Binding Pipe.Path}"
TextureCoordinates="{Binding Pipe.TextureCoordinates}"
Diameter="{Binding ElementName=PipeDiamSlider, Path= Value }"
Material="{Binding Pipe.Material}"
BackMaterial="{Binding Pipe.Material}"
ThetaDiv="50" IsPathClosed="False"
Visible="{Binding ElementName=PipeIsVisibleCheck, Path=IsChecked}"/>
Where “TubeVisual3D” is a 3D WPF element defined in Helix Toolkit and most of the parameters bound by the binding are dependency properties in “TubeVisual3D”.
The equivalent to your markup would be this:
HelixToolkit.Wpf.TubeVisual3D pipeVisual = new HelixToolkit.Wpf.TubeVisual3D();
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.ExtrudedVisual3D.PathProperty, new Binding("Pipe.Path"));
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.ExtrudedVisual3D.TextureCoordinatesProperty, new Binding("Pipe.TextureCoordinates"));
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.TubeVisual3D.DiameterProperty, new Binding("Value") { Source = PipeDiamSlider });
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.MeshElement3D.MaterialProperty, new Binding("Pipe.Material"));
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.MeshElement3D.BackMaterialProperty, new Binding("Pipe.Material"));
pipeVisual.ThetaDiv = 50;
pipeVisual.IsPathClosed = false;
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.MeshElement3D.VisibleProperty, new Binding("IsChecked") { Source = PipeIsVisibleCheck });
Related
Is there an easy/straightforward way to dynamically add (not edit the value of) multiple checkbox controls in a .docx document body?
I tried appending a single SdtContentCheckBox after a new paragraph like this but with no luck:
newParagraph.Append(new SdtContentCheckBox());
and also followed the instructions here:
https://www.codeproject.com/Tips/370758/Add-dynamic-content-controls-to-a-word-document and here: How do I create a check box in C# using Open XML SDK
The first one showed only how to add a text content control and the second one straight up resulted in a corrupted .docx file.
Any help would be appreciated!
Closest working code I could find was this:
https://social.msdn.microsoft.com/Forums/office/en-US/f6ce8ecf-0ed8-4f18-958a-a086f212d1e2/how-to-create-a-checked-checkbox-form-field-using-the-sdk?forum=oxmlsdk
public static Paragraph GenerateParagraph()
{
var element =
new Paragraph(
new Run(
new FieldChar(
new FormFieldData(
new FormFieldName(){ Val = "Check1" },
new Enabled(),
new CalculateOnExit(){ Val = BooleanValues.Zero },
new CheckBox(
new AutomaticallySizeFormField(),
new DefaultCheckboxFormFieldState(){ Val = BooleanValues.Zero }))
){ FieldCharType = FieldCharValues.Begin }),
new BookmarkStart(){ Name = "Check1", Id = 0 },
new Run(
new FieldCode(" FORMCHECKBOX "){ Space = "preserve" }),
new Run(
new FieldChar(){ FieldCharType = FieldCharValues.End }),
new BookmarkEnd(){ Id = 0 },
new Run(
new Text("My check box"))
){ RsidParagraphAddition = "00784880", RsidRunAdditionDefault = "00B77989" };
return element;
}
Using this I was able to dynamically add Legacy Checkboxes (i.e. neither Content control nor ActiveX control), but at least it is a start!
If someone knows how to add Checkbox Content controls, feel free to post a reply below and I'll mark it as Correct.
Even though you found yourself the answer, I'll leave this here in case anyone stumbles upon this looking for something related.
There's a tool called Open XML SDK 2.5 Productivity Tool, which you can download from here that allows you to reverse-engineer a word .docx document to obtain the C# code to generate it from scratch.
In order to get the code that you are looking for to generate any kind of word element (a checkbox, a table, a bulleted list...), you need to create a word document with said element and save it.
Then, open it using the Open XML SDK 2.5 Productivity Tool and click on the "Reflect Code" button. The generated code will show you how to create those elements, styles and other formatting included.
With that, I got the code necessary to get a paragraph with a checkbox
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
public static Paragraph GenerateCheckboxParagraph(string internalName, int internalId, string textAfterTextbox)
{
var run1 = new Run(
new FieldChar(
new FormFieldData(
new FormFieldName() { Val = internalName },
new Enabled(),
new CalculateOnExit() { Val = OnOffValue.FromBoolean(false) },
new CheckBox(
new AutomaticallySizeFormField(),
new DefaultCheckBoxFormFieldState() { Val = OnOffValue.FromBoolean(false) }))
)
{
FieldCharType = FieldCharValues.Begin
}
);
var run2 = new Run(new FieldCode(" FORMCHECKBOX ") { Space = SpaceProcessingModeValues.Preserve });
var run3 = new Run(new FieldChar() { FieldCharType = FieldCharValues.End });
var run4 = new Run(new Text(textAfterTextbox));
var element =
new Paragraph(
run1,
new BookmarkStart() { Name = internalName, Id = new StringValue(internalId.ToString()) },
run2,
run3,
new BookmarkEnd() { Id = new StringValue(internalId.ToString()) },
run4
);
return element;
}
I am trying to learn WPF, the problem is I need to learn XAML first. In System.Windows.Controls.TextBox class, there is property 'FontFamily'. I will have to set a font for it. In one of my book I saw But where can I get this font(Verdana). Where is list of all font's in MSDN Library?
Ikbal Hassan
You can bind to the string name of the font as shown below as font.Name. The following code is loading up a ComboBox of all the fonts.
var installedfonts = new InstalledFontCollection();
int idFont = 0;
foreach ( var font in installedfonts.Families )
{
var fonttype = new LookupSelectItem { Id = idFont, Name = font.Name };
FontTypeList.Add( fonttype );
++ idFont;
}
I have a DataTable that is coming from a Web Service, which I need to bind to a ComboBox. I have not grokked doing binding in XAML yet so this question is about binding in code instead. So far I have tried
cboManager.DataContext = Slurp.DistrictManagerSelect().DefaultView;
cboManager.DisplayMemberPath = "Name";
cboManager.SelectedValuePath = "NameListId";
cboManager.SetBinding(ComboBox.ItemsSourceProperty, new Binding());
And I have tried
DataTable tbl = new DataTable();
tbl = Slurp.DistrictManagerSelect();
cboManager.ItemsSource = ((IListSource)tbl).GetList();
cboManager.DisplayMemberPath = "[Name]";
cboManager.SelectedValuePath = "[NameListId]";
DataContext = this;
In both cases I get the list of managers to show but when I select from the ComboBox I get [Name] and [NameListId] and not the values I am expecting. What am I doing wrong (other than not using XAML's DataBinding)?
Edit added after answers to my original post came in.
So (based on Rachel's response) try number three looks like this:
using (DataTable tbl = Slurp.DistrictManagerSelect())
{
List<ManagerList> list = new List<ManagerList>();
foreach (var row in tbl.Rows)
{
list.Add(new ManagerList
{
NameListId = (int)row[0],
Name = row[1].ToString()
});
}
}
Assuming I am doing what she meant the correct way I am no getting this error Cannot apply indexing with [] to an expression of type 'object'
Have you tried to just bind to the DataTable directly? Do you have columns Name and NameListId? Leave off the DataContext (you already assigned the ItemsSource).
DataTable tbl = Slurp.DistrictManagerSelect();
cboManager.ItemsSource = tbl;
cboManager.DisplayMemberPath = "Name";
cboManager.SelectedValuePath = "NameListId";
When you cast to the IListSource I suspect it is combining all the columns. If you want to bind to a list then you need to create items that have properties Name and NameListID.
I think that's because your ItemsSource is a DataTable, so each ComboBoxItem contains a DataContext of a DataRow, and the DataRow class doesn't have properties called Name or NameListId
Basically, you're trying to tell the ComboBox to display DataRow.Name, and set the value to DataRow.NameListId, both of which are not valid properties.
I usually prefer to parse data into objects, and bind the ItemsSource a List<MyObject> or ObservableCollection<MyObject>
foreach(DataRow row in tbl.Rows)
list.Add(new MyObject { Name = row[0].ToString(), NameListId = (int)row[1] });
cboManager.ItemsSource = list;
I see that the .Net XamlWriter is not available in Silverlight. Well - I need one anyway, so I assume there is a solution to this..?
I have some UIElement objects (Path, Ellipse, Rectangle, ..), and I want to store their Xaml definition such that I can load these later using XamlWriter.Load(). Any ideas on how to do this? Any 3rdParty XamlWriter implementations etc that are recommended?
There seems to be some implementations of XamlWriter for Silverlight around. The one I've seen which looks most serious is in Silverlight Contrib, but this is not yet supported for SL3, which I'm using.
Since I only had a few specific objects to extract xaml from I created the functions to do so myself. Some more refactoring will be done, but this one works for finding the xaml for my path drawing - stored as a InkPresenter:
public static string ConvertPathToXaml(InkPresenter drawObject)
{
string xmlnsString = "http://schemas.microsoft.com/client/2007";
XNamespace xmlns = xmlnsString;
var strokes = new XElement(xmlns + "StrokeCollection");
foreach (var strokeData in drawObject.Strokes)
{
var stroke = new XElement(xmlns + "Stroke",
new XElement(xmlns + "Stroke.DrawingAttributes",
new XElement(xmlns + "DrawingAttributes",
new XAttribute("Color", strokeData.DrawingAttributes.Color),
new XAttribute("OutlineColor", strokeData.DrawingAttributes.OutlineColor),
new XAttribute("Width", strokeData.DrawingAttributes.Width),
new XAttribute("Height", strokeData.DrawingAttributes.Height))));
var points = new XElement(xmlns + "Stroke.StylusPoints");
foreach (var pointData in strokeData.StylusPoints)
{
var point = new XElement(xmlns + "StylusPoint",
new XAttribute("X", pointData.X),
new XAttribute("Y", pointData.Y));
points.Add(point);
}
stroke.Add(points);
strokes.Add(stroke);
}
var strokesRoot = new XElement(xmlns + "InkPresenter.Strokes", strokes);
var inkRoot = new XElement(xmlns + "InkPresenter", new XAttribute("xmlns", xmlnsString),
new XAttribute("Opacity", drawObject.Opacity), strokesRoot);
return inkRoot.ToString();
}
I want to use the GridView mode of a ListView to display a set of data that my program will be receiving from an external source. The data will consist of two arrays, one of column names and one of strings values to populate the control.
I don't see how to create a suitable class that I can use as the Item in a ListView. The only way I know to populate the Items is to set it to a class with properties that represent the columns, but I have no knowledge of the columns before run-time.
I could create an ItemTemplate dynamically as described in: Create WPF ItemTemplate DYNAMICALLY at runtime but it still leaves me at a loss as to how to describe the actual data.
Any help gratefully received.
You can add GridViewColumns to the GridView dynamically given the first array using a method like this:
private void AddColumns(GridView gv, string[] columnNames)
{
for (int i = 0; i < columnNames.Length; i++)
{
gv.Columns.Add(new GridViewColumn
{
Header = columnNames[i],
DisplayMemberBinding = new Binding(String.Format("[{0}]", i))
});
}
}
I assume the second array containing the values will be of ROWS * COLUMNS length. In that case, your items can be string arrays of length COLUMNS. You can use Array.Copy or LINQ to split up the array. The principle is demonstrated here:
<Grid>
<Grid.Resources>
<x:Array x:Key="data" Type="{x:Type sys:String[]}">
<x:Array Type="{x:Type sys:String}">
<sys:String>a</sys:String>
<sys:String>b</sys:String>
<sys:String>c</sys:String>
</x:Array>
<x:Array Type="{x:Type sys:String}">
<sys:String>do</sys:String>
<sys:String>re</sys:String>
<sys:String>mi</sys:String>
</x:Array>
</x:Array>
</Grid.Resources>
<ListView ItemsSource="{StaticResource data}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=[0]}" Header="column1"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=[1]}" Header="column2"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=[2]}" Header="column3"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
Thanks, that is very helpful.
I used it to create a dynamic version as follows. I created the column headings as you suggested:
private void AddColumns(List<String> myColumns)
{
GridView viewLayout = new GridView();
for (int i = 0; i < myColumns.Count; i++)
{
viewLayout.Columns.Add(new GridViewColumn
{
Header = myColumns[i],
DisplayMemberBinding = new Binding(String.Format("[{0}]", i))
});
}
myListview.View = viewLayout;
}
Set up the ListView very simply in XAML:
<ListView Name="myListview" DockPanel.Dock="Left"/>
Created an wrapper class for ObservableCollection to hold my data:
public class MyCollection : ObservableCollection<List<String>>
{
public MyCollection()
: base()
{
}
}
And bound my ListView to it:
results = new MyCollection();
Binding binding = new Binding();
binding.Source = results;
myListview.SetBinding(ListView.ItemsSourceProperty, binding);
Then to populate it, it was just a case of clearing out any old data and adding the new:
results.Clear();
List<String> details = new List<string>();
for (int ii=0; ii < externalDataCollection.Length; ii++)
{
details.Add(externalDataCollection[ii]);
}
results.Add(details);
There are probably neater ways of doing it, but this is very useful for my application. Thanks again.
This article on the CodeProject explains exactly how to create a Dynamic ListView - when data is known only at runtime.
http://www.codeproject.com/KB/WPF/WPF_DynamicListView.aspx
Not sure if it's still relevant but I found a way to style the individual cells with a celltemplate selector. It's a bit hacky because you have to munch around with the Content of the ContentPresenter to get the proper DataContext for the cell (so you can bind to the actual cell item in the cell template):
public class DataMatrixCellTemplateSelectorWrapper : DataTemplateSelector
{
private readonly DataTemplateSelector _ActualSelector;
private readonly string _ColumnName;
private Dictionary<string, object> _OriginalRow;
public DataMatrixCellTemplateSelectorWrapper(DataTemplateSelector actualSelector, string columnName)
{
_ActualSelector = actualSelector;
_ColumnName = columnName;
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
// The item is basically the Content of the ContentPresenter.
// In the DataMatrix binding case that is the dictionary containing the cell objects.
// In order to be able to select a template based on the actual cell object and also
// be able to bind to that object within the template we need to set the DataContext
// of the template to the actual cell object. However after the template is selected
// the ContentPresenter will set the DataContext of the template to the presenters
// content.
// So in order to achieve what we want, we remember the original DataContext and then
// change the ContentPresenter content to the actual cell object.
// Therefor we need to remember the orginal DataContext otherwise in subsequent calls
// we would get the first cell object.
// remember old data context
if (item is Dictionary<string, object>)
{
_OriginalRow = item as Dictionary<string, object>;
}
if (_OriginalRow == null)
return null;
// get the actual cell object
var obj = _OriginalRow[_ColumnName];
// select the template based on the cell object
var template = _ActualSelector.SelectTemplate(obj, container);
// find the presenter and change the content to the cell object so that it will become
// the data context of the template
var presenter = WpfUtils.GetFirstParentForChild<ContentPresenter>(container);
if (presenter != null)
{
presenter.Content = obj;
}
return template;
}
}
Note: I changed the DataMatrix frome the CodeProject article so that rows are Dictionaries (ColumnName -> Cell Object).
I can't guarantee that this solution will not break something or will not break in future .Net release. It relies on the fact that the ContentPresenter sets the DataContext after it selected the template to it's own Content. (Reflector helps a lot in these cases :))
When creating the GridColumns, I do something like that:
var column = new GridViewColumn
{
Header = col.Name,
HeaderTemplate = gridView.ColumnHeaderTemplate
};
if (listView.CellTemplateSelector != null)
{
column.CellTemplateSelector = new DataMatrixCellTemplateSelectorWrapper(listView.CellTemplateSelector, col.Name);
}
else
{
column.DisplayMemberBinding = new Binding(string.Format("[{0}]", col.Name));
}
gridView.Columns.Add(column);
Note: I have extended ListView so that it has a CellTemplateSelector property you can bind to in xaml
#Edit 15/03/2011:
I wrote a little article which has a little demo project attached: http://codesilence.wordpress.com/2011/03/15/listview-with-dynamic-columns/
Totally progmatic version:
var view = grid.View as GridView;
view.Columns.Clear();
int count=0;
foreach (var column in ViewModel.GridData.Columns)
{
//Create Column
var nc = new GridViewColumn();
nc.Header = column.Field;
nc.Width = column.Width;
//Create template
nc.CellTemplate = new DataTemplate();
var factory = new FrameworkElementFactory(typeof(System.Windows.Controls.Border));
var tbf = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock));
factory.AppendChild(tbf);
factory.SetValue(System.Windows.Controls.Border.BorderThicknessProperty, new Thickness(0,0,1,1));
factory.SetValue(System.Windows.Controls.Border.MarginProperty, new Thickness(-7,0,-7,0));
factory.SetValue(System.Windows.Controls.Border.BorderBrushProperty, Brushes.LightGray);
tbf.SetValue(System.Windows.Controls.TextBlock.MarginProperty, new Thickness(6,2,6,2));
tbf.SetValue(System.Windows.Controls.TextBlock.HorizontalAlignmentProperty, column.Alignment);
//Bind field
tbf.SetBinding(System.Windows.Controls.TextBlock.TextProperty, new Binding(){Converter = new GridCellConverter(), ConverterParameter=column.BindingField});
nc.CellTemplate.VisualTree = factory;
view.Columns.Add(nc);
count++;
}
I would go about doing this by adding an AttachedProperty to the GridView where my MVVM application can specify the columns (and perhaps some additional metadata.) The Behavior code can then dynamically work directly with the GridView object to create the columns. In this way you adhere to MVVM and the ViewModel can specify the columns on the fly.