How can I create this from code ?
<CheckBox Command="{Binding Source={StaticResource VMLocator}, Path=TimeTableInformationViewModel.MyCommand }"
CommandParameter="{Binding valueFromInput}" />
I am not sure how to set Command property from behind code :
public static DataTemplate CreateDataTemplate()
{
var block = new FrameworkElementFactory(typeof(CheckBox));
block.SetBinding(CheckBox.CommandProperty, new Binding(""));
DataTemplate newDataTemplate = new DataTemplate() { VisualTree = block };
}
Try this:
TypeOfYourObject vmLocator = (TypeOfYourObject)Resources["VMLocator"];
CheckBox checkBox = new CheckBox();
checkBox.Command = vmLocator.TimeTableInformationViewModel.MyCommand;
checkBox.CommandParameter = vmLocator.valueFromInput;
UPDATE >>>
There are a number of ways of doing this, but I have included a simple example that includes setting up a Binding... for more than this, please refer to the How do I build a DataTemplate in c# code? post to find out how to create a larger DataTemplate in code.
FrameworkElementFactory checkbox = new FrameworkElementFactory(typeof(CheckBox));
checkBox.Name = "aCheckBox";
checkBox.SetBinding(TextBlock.IsCheckedProperty, new Binding("YourBoolProperty"));
DataTemplate dataTemplate = new DataTemplate() { VisualTree = checkbox };
Related
Evening All,
Learning Xamarin forms..attempting to add a picker with numeric values...(using https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource)
I have used the example on this page to get the picker populated from the view...which works fine...however I want to populate the picker from code behind...
<---XAML--->
<Picker Grid.Column="4" Grid.Row="2" ItemsSource="{Binding pickerSource}"/>
<---c#---->
var pickerList = new List<string>();
pickerList.Add("1");
pickerList.Add("2");
pickerList.Add("3");
pickerList.Add("4");
pickerList.Add("5");
pickerList.Add("6");
pickerList.Add("7");
pickerList.Add("8");
pickerList.Add("9");
pickerList.Add("10");
var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
pickerSource.ItemsSource = pickerList;
Picker is appearing on app but when selected, its not populated with any values...why isnt this binding properly anyone?
Thank you
Also...as a side note if anyone is aware of a tool that contains all numeric values instead of me manually having to populate it with 1,2,3 etc..
Thanks Again
Thanks to #Jason for the reply...From here I have went with the following:
---xaml--
<Picker Grid.Column="4" Grid.Row="2" ItemsSource="{Binding pickerSource}"/>
---c#----
public List<string> pickerSource { get; set; }
public void PopulateQuantityPicker()
{
var pickerList = new List<string>();
pickerList.Add("1");
pickerList.Add("2");
pickerList.Add("3");
pickerList.Add("4");
pickerList.Add("5");
pickerList.Add("6");
pickerList.Add("7");
pickerList.Add("8");
pickerList.Add("9");
pickerList.Add("10");
pickerSource = pickerList;
this.BindingContext = this;
}
The picker is on the app, but it is not populated, it is empty.
When I Click on it I get the following:
(also the code is hitting the PopulateQuantityPicker())
here you are binding your ItemsSource to pickerSource
<Picker Grid.Column="4" Grid.Row="2" ItemsSource="{Binding pickerSource}"/>
in your code behind, you need a public property named pickerSource. You can only bind to public properties
public List<string> pickerSource { get; set }
// assign the data to your ItemsSource
pickerSource = pickerList;
// also be sure to set the BindingContext
BindingContext = this;
// this is creating a new picker named pickerSource. You have already done
// this in your XAML. This is NOT NEEDED
var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
pickerSource.ItemsSource = pickerList;
if you want to do this from the code behind WITHOUT using binding, you first need to assign an x:name to your control
<Picker x:Name="myPicker" Grid.Column="4" Grid.Row="2" />
then in the code behind
myPicker.ItemsSource = pickerList;
How to: Fill a ListView by using C# instead of using XAML
I would like to fill a ListView by using C# (WPF), not by using XAML. The reason for this is, that we do not know the number of elements before runtime.
This is my working XAML code:
<ListView Name="listView_BusinessContacts" SelectionMode="Single">
<ListViewItem Selected="ListViewItem_Selected">
<DockPanel DockPanel.Dock="Top" Name="dockPanel_1">
<Image DockPanel.Dock="Left" Source="/X;component/Images/folder.png" Stretch="None" />
<Label Content="Test 123" DockPanel.Dock="Right" Name="label_1" />
</DockPanel>
</ListViewItem>
</ListView>
My idea is first to create the ListViewItem. After that, I could create the DockPanel. But now, I do not know, how to add two elements to a DockPanel (here: Image and Label). After that, I would add the DockPanel to the ListViewItem and than I would add that ListViewItem to the ListView.
I hope, that you understand what I want to do.
Solution by SynerCoder:
public void SetListViewItems()
{
foreach (var item in File.ReadAllLines(#"C:\Companies\Companies.txt", Encoding.UTF8))
{
Image image = new Image();
image.Source = new BitmapImage(new Uri(#"Images\folder.png", UriKind.Relative));
image.Stretch = Stretch.None;
Label label = new Label();
label.Content = "Test 123";
DockPanel.SetDock(image, Dock.Left);
DockPanel.SetDock(label, Dock.Right);
DockPanel dockPanel = new DockPanel();
dockPanel.Children.Add(image);
dockPanel.Children.Add(label);
ListViewItem listViewItem = new ListViewItem();
listViewItem.Content = dockPanel;
listView_BusinessContacts.Items.Add(listViewItem);
}
}
You can use the following code to create your listview, use the static SetDock method of the DockPanel to set the docking positions.
var listView = new ListView();
foreach (var item in something)
{
var image = new Image();
image.Source = ...;
image.Stretch = Stretch.None;
var label = new Label();
label.Content = "Test 123";
DockPanel.SetDock(image, Dock.Left);
DockPanel.SetDock(label, Dock.Right);
var dockPanel = new DockPanel();
dockPanel.Children.Add(image);
dockPanel.Children.Add(label);
var item = new ListViewItem();
item.Content = dockPanel;
listView.Items.Add(item);
}
I want to add a number of TextBlocks inside a Button. How can I add a number of them, along with StackPanels or Canvases, in C#, as shown below in XAMAL
<Button>
<StackPanel>
<TextBlock Text="ABC"/>
<TextBlock Text="DEF"/>
</StackPanel>
</Button>
It's easy:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
var tb1 = new TextBlock() { Text = "TextBlock 1" };
var tb2 = new TextBlock() { Text = "TextBlock 2" };
var stackPanel = new StackPanel();
stackPanel.Children.Add(tb1);
stackPanel.Children.Add(tb2);
var button = new Button() { Content = stackPanel };
this.Content = button;
}
}
Maybe you should think about an enclosing control of csharpfolk's answer. This would help to get a reuseable control.
The text strings are good to use as a dependency property. :)
Regards,
- Tobbo
I just want to add a ToolTip for each item in a Silverlight Custom ComboBox. so when a user moves around items can see ToolTip, items in ComboBox will be of type string and the same value will be displayed as ToolTip.
You can create a tooltip by adding a simple data template -- give the template TextBlock with the tool tip added:
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock ToolTip="{Binding}" Text="{Binding}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Edit - Using Code behind
var dt = new DataTemplate();
var tb = new TextBlock();
tb.ToolTip = new Binding(".");
tb.Text = new Binding(".");
dt.VisualTree = tb;
var cb = new ComboBox();
cb.ItemTemplate = dt;
Here is my code:
foreach (var columnData in lookup.DataProvider.Metadata)
{
DataGridColumn column = new DataGridTextColumn { Binding = new Binding(columnData.FieldName) };
if (columnData.DataType == typeof(bool))
{
column = new DataGridCheckBoxColumn { Binding = new Binding(columnData.FieldName) };
}
if (columnData.DataType == typeof(DateTime))
{
column = new DataGridTemplateColumn();
//... ????
}
column.Header = columnData.Caption;
DataDataGrid.Columns.Add(column);
}
Basically, I'm creating columns and bindings in code because columns not known at design-time.
Now I need to add templated column and not sure how to write it in C#. Here is example of XAML of column I need to add:
<sdk:DataGridTemplateColumn Header="Received" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" SortMemberPath="SomeTime">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<MyControls:MyDateTimeLabel DisplayUtcDate="{Binding SomeTime}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
EDIT
In case someone interested. I used solution from here: http://www.pettijohn.com/2011/01/silverlight-datagrid-with-dynamic.html
I took version with XAML loader. It definitely smells since I got my namespaces etc hardcoded into strings.
So, I started to explore second choice. Here is how my dynamic column looks now:
column = new DataGridTemplateColumn
{
CanUserSort = true,
SortMemberPath = columnData.FieldName,
CellTemplate = (DataTemplate)this.Resources["DateTimeColumnDataTemplate"]
};
I'm loading DateTemplate from resources. This was cool, but how do I do binding? Suggestion here was to get to my DateTimeLabel and set binding. But that didn't work(see article on why). So, I wrote this code and all is well:
private void OnLoadingRow(object sender, DataGridRowEventArgs e)
{
foreach (DataGridColumn t in this.DataDataGrid.Columns)
{
if (t is DataGridTemplateColumn)
{
var label = t.GetCellContent(e.Row) as DitatDateTimeLabel;
label.SetBinding(DitatDateTimeLabel.DisplayUtcDateProperty, new Binding(t.SortMemberPath));
}
}
}
You could put your DataTemplate inside Page/UserControl resources, retrieve it in code and apply to your column's CellTemplate. It would look sth like this:
column.CellTemplate = (DataTemplate)this.Resources["DateTimeFieldTemplate"];
The binding should work as it is in your DataTemplate XAML right now because on the DataGrid row level your DataContext will be set to the item itself.