Hi I am using TabControl to make Conference System, the private chats would be in a new TabItem. I am using this code to present a new member chatting in private:
TabItem ti = new TabItem();
ti.Header="Name";
MyTabControl.Items.Add(ti);
but the problem with this code is that I am adding a list box in the TabItem but the TabItem doesn't have a function to add an item in it. how can I add items into TabItem?
Second try: I used this code to present a new member chatting in private
ItemsControl it = new ItemsControl();
ListBox lst = new ListBox();
lst.Width = 571;
lst.Height = 301;
it.Items.Add(lst);
tabControlChat.Items.Add(it);
with this code I can add all items I need in the new tab. but the main problem is that I can't name the tab there is no property like (ti.Header) to name the tab. so what is the solution please? and thank you
For short:
ListBox lb = new ListBox();
lb.Items.Add("chat member");
TabItem ti = new TabItem();
ti.Header = "Private Chats";
ti.Content = lb;
TabControl tc = new TabControl();
tc.Items.Add(ti);
Use this:
TabItem ti = new TabItem();
ti.Header = "Name";
tabControl1.Items.Add(ti);
ListBox lst = new ListBox();
lst.Width = 571;
lst.Height = 301;
ti.Content = lst;
Related
I want to create an array of an UserControl and add it dynamically into a tabitems.
Here is how I proceed :
UserControl1[] UC1 = new UserControl1[it1];
TabItem[] tabItems = new TabItem[it1];
tabItems[0].Content = UC1[0];
_MyTabControl.Items.Add(tabItems[0]);
But this doesn't work. How can I do ?
You need to add at least one UserControl and one TabItem to the arrays. This works:
int it1 = 1;
UserControl1[] UC1 = new UserControl1[it1];
UC1[0] = new UserControl1();
TabItem[] tabItems = new TabItem[it1];
tabItems[0] = new TabItem();
tabItems[0].Content = UC1[0];
_MyTabControl.Items.Add(tabItems[0]);
I'm dynamically updating a ListView like that:
ListViewItem item = new ListViewItem();
item.Text = "Text1";
item.SubItems.Add("Text2");
item.SubItems.Add("Text3");
item.SubItems.Add("Text4");
item.Tag = i;
listView.Items.Add(item);
now I want that instead of Text4 will be a lil icon I will get dynamically from a url. I read a lot of threads and tried many things - but I can't get this to work..
You need to implement ImageList in your function.
Code :
// get picture resource
WebClient _web = new WebClient();
byte[] _data = _wb.DownloadData("http://www.myzony.com/usr/uploads/2017/03/3197402477.png");
MemoryStream _ms = new MemoryStream(_data);
// Loaded to imagelist
ImageList list = new ImageList();
list.Images.Add("pic1",Image.FromStream(_ms));
// bind listview
listView1.SmallImageList = list;
ListViewItem _item1 = new ListViewItem();
_item1.Text = "Test";
_item1.SubItems.Add("Test2");
_item1.SubItems.Add("pic1");
_item1.ImageKey = "pic1";
listView1.Items.Add(_item1);
Effect Image:
I'm working on a cookbook for myself, written in WPF/C#.
Additionally I'm new to Data Bindings.
My Problem is, I want to generate a Datagrid in a TabItem on runtime in code behind, including Bindings. I can't set a Datagrid at XAML because I want to create all TabItems dynamically.
Following Code so far:
XAML:
<UniformGrid Columns="2" Rows="1">
<TabControl Name="TabControl" TabStripPlacement="Left"/>
<TabItem Header= "first dish" Name = "firstdish"/>
</UniformGrid>
XAML.cs for generation:
//New Grid
var Grid = new DataGrid();
//Start Test list creation with three items
var TestList = new List<Receipt>();
//Set binding
Grid.ItemsSource = TestList;
var Rec = new Receipt();
Rec.Creator = "DaJohn1";
Rec.ID = 1;
Rec.Title = "TestReceipt1";
var Rec2 = new Receipt();
Rec2.Creator = "DaJohn2";
Rec2.ID = 2;
Rec2.Title = "TestReceipt2";
var Rec3 = new Receipt();
Rec3.Creator = "DaJohn3";
Rec3.ID = 3;
Rec3.Title = "TestReceipt3";
TestList.Add(Rec);
TestList.Add(Rec2);
TestList.Add(Rec3);
//End Test list creation
//Add Column
var SingleColumn = new DataGridTextColumn();
Grid.Columns.Add(SingleColumn);
SingleColumn.Binding = new Binding("Creator");
SingleColumn.Header = "Creator";
//Add Column
var SingleColumn2 = new DataGridTextColumn();
Grid.Columns.Add(SingleColumn2);
SingleColumn2.Binding = new Binding("Title");
SingleColumn2.Header = "Title";
//Set tabitem content to datagrid
firstdish.Content = Grid;
All I'm getting is an datagrid with four rows (looks like the count of Items is right), which are all empty, no data to be seen.
I'm staring at this since last Weeks Monday and can't find an answer anywhere.
Thanks for any ideas and solutions.
try changing
var TestList = new List<Receipt>();
to
var TestList = new ObservableCollection<Receipt>();
as it automatically notifies UI about changes.
I had a similar problem with items not rendering so this may be it.
I've taken your code and checked some of the things you do: The main error is that in your xaml code you created the Tab Control and then Put the TabItem outside it, Tabitem must be inside the control for it to work correctly. so First error is:
<TabControl Name="TabControl" TabStripPlacement="Left">
<TabItem Header="First dish" Name = "firstdish" />
</TbControl>
Other things you need to do before going on in my opinion are:
Assign your window or other UI element itself as datacontext
Verify that your Receipt class implements the INotifyPropertyChanged event that has to be raised by all its properties, or implement your properties as DependencyProperties (even if the latter is not necessary)
Transform your datasource TestList in an ObservableCollection
Create TestList as a class property not a local variable so that it is into the datacontext
I've made a small sample using the above indication and your code to let you see better how that works. You can download the zip here:
TestClassDataGrid.zip
Below code should work for you,
XAML:
<TabControl Name="TabControl" TabStripPlacement="Left">
<TabItem Header= "first dish" Name = "firstdish"/>
</TabControl>
.cs file
public Window()
{
InitializeComponent();
var Grid = new DataGrid();
//Start Test list creation with three items
var TestList = new List<Receipt>();
//Set binding
Grid.ItemsSource = TestList;
var Rec = new Receipt();
Rec.Creator = "DaJohn1";
Rec.ID = 1;
Rec.Title = "TestReceipt1";
var Rec2 = new Receipt();
Rec2.Creator = "DaJohn2";
Rec2.ID = 2;
Rec2.Title = "TestReceipt2";
var Rec3 = new Receipt();
Rec3.Creator = "DaJohn3";
Rec3.ID = 3;
Rec3.Title = "TestReceipt3";
TestList.Add(Rec);
TestList.Add(Rec2);
TestList.Add(Rec3);
//End Test list creation
//Add Column
var SingleColumn = new DataGridTextColumn();
Grid.Columns.Add(SingleColumn);
SingleColumn.Binding = new Binding("Creator");
SingleColumn.Header = "Creator";
//Add Column
var SingleColumn2 = new DataGridTextColumn();
Grid.Columns.Add(SingleColumn2);
SingleColumn2.Binding = new Binding("Title");
SingleColumn2.Header = "Title";
//Set tabitem content to datagrid
Grid.AutoGenerateColumns = false;
firstdish.Content = Grid;
}
In my C# WPF application I programmatically add a ComboBoxColumn to a DataGrid:
public static DataGridComboBoxColumn getCboCol(string colName, Binding textBinding)
{
List<string> statusItemsList = new StatusStrList();
DataGridComboBoxColumn cboColumn = new DataGridComboBoxColumn();
cboColumn.Header = colName;
cboColumn.SelectedItemBinding = textBinding;
cboColumn.ItemsSource = statusItemsList;
return cboColumn;
}
If an item in the containing DataGrid contains text, which my StatusStrList doesn't contain, it won't be displayed.
Example: If my StatusStrList contains A, B, C and a DataGrid's item has X, the X won't be displayed as text in the ComboBox.
How can I fix this?
Thanks in advance,
Christian
DataGridComboBoxColumn isn't dynamic enough to do something like this but you can use DataGridTemplateColumn. Code below should achieve the functionality you need. It works by using a CellTemplate containing a TextBlock which easily displays an item that wouldn't be in the ItemsSource of the ComboBox. Going into edit mode will bring up the ComboBox that contains all of the items of the list.
DataGridTemplateColumn cboColumn = new DataGridTemplateColumn();
cboColumn.Header = colName;
//DataTemplate for CellTemplate
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory txtBlkFactory = new FrameworkElementFactory(typeof(TextBlock));
txtBlkFactory.SetValue(TextBlock.TextProperty, textBinding);
cellTemplate.VisualTree = txtBlkFactory;
cboColumn.CellTemplate = cellTemplate;
//DataTemplate for CellEditingTemplate
DataTemplate editTemplate = new DataTemplate();
FrameworkElementFactory cboFactory = new FrameworkElementFactory(typeof(ComboBox));
cboFactory.SetValue(ComboBox.TextProperty, textBinding);
cboFactory.SetValue(ComboBox.ItemsSourceProperty, statusItemsList);
cboFactory.SetValue(ComboBox.IsEditableProperty, true);
MouseEventHandler handler = new MouseEventHandler(delegate(object sender, MouseEventArgs args)
{
ComboBox cboBox = (ComboBox)sender;
cboBox.IsDropDownOpen = true;
});
cboFactory.AddHandler(ComboBox.MouseEnterEvent, handler);
editTemplate.VisualTree = cboFactory;
cboColumn.CellEditingTemplate = editTemplate;
I am trying to create a generic view, and I want it to contain a ListBox with a datatemplate
I want to create it either using pure C# code or if possible load it through xaml? If I can create a template I can get in c# as a resource of sorts.
What I have made until now is
private static ListBox CreateDayListBox()
{
var listBox = new ListBox();
var dataTemplate = new DataTemplate();
var grid = new Grid();
var columnDefinition1 = new ColumnDefinition {Width = GridLength.Auto};
var columnDefinition2 = new ColumnDefinition();
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
var rectangleItemBought = new Rectangle {Width = 50, Height = 50};
rectangleItemBought.SetBinding(Rectangle.FillProperty, new Binding("Bought"));
grid.Children.Add(rectangleItemBought);
var textBlockItemName = new TextBlock();
textBlockItemName.SetBinding(TextBlock.TextProperty, new Binding("Name"));
var textBlockItemQuantity = new TextBlock();
textBlockItemQuantity.SetBinding(TextBlock.TextProperty, new Binding("Quantity"));
var textBlockItemQuantityType = new TextBlock();
textBlockItemQuantityType.SetBinding(TextBlock.TextProperty, new Binding("QuantityType"));
var stackpanel = new StackPanel();
Grid.SetColumn(stackpanel, 1);
stackpanel.Children.Add(textBlockItemName);
stackpanel.Children.Add(textBlockItemQuantity);
stackpanel.Children.Add(textBlockItemQuantityType);
grid.Children.Add(stackpanel);
return listBox;
}
So I want the listbox datatemplate to contain 1 rectangle, 1 stackpanel with 3 textboxes inside
You can write the template in XAML then load it in your code.
Read this.
Besides, I'm pretty sure you can create the DataTemplate by code the same way you created the controls, look at this code (credit):
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory =
new FrameworkElementFactory(typeof(StackPanel));
template.VisualTree = factory;
FrameworkElementFactory childFactory =
new FrameworkElementFactory(typeof(Image));
childFactory.SetBinding(Image.SourceProperty, new Binding("Machine.Thumbnail"));
childFactory.SetValue(Image.WidthProperty, 170.0);
childFactory.SetValue(Image.HeightProperty, 170.0);
factory.AppendChild(childFactory);
childFactory = new FrameworkElementFactory(typeof(Label));
childFactory.SetBinding(Label.ContentProperty,
new Binding("Machine.Descriiption"));
childFactory.SetValue(Label.WidthProperty, 170.0);
childFactory.SetValue(Label.HorizontalAlignmentProperty,
HorizontalAlignment.Center);
factory.AppendChild(childFactory);