My goal is to have a viewable text that can say what I want and have the value of that text be used for the onchange event. However, I can't seem to get the datasource to attach to the combobox.
<ComboBox x:Name="WPFDomainUsersBox" HorizontalAlignment="Left" Margin="288,10,0,0" VerticalAlignment="Top" Width="215" Height="23" Text="Domain Users"/>
...
...
...
#create a datatable to bind to our combobox
$datatable = New-Object system.Data.DataTable
#Define Columns
$ColValue = New-Object system.Data.DataColumn "Value",([string])
$ColText = New-Object system.Data.DataColumn "Text",([string])
#add columns to datatable
$datatable.columns.add($ColValue)
$datatable.columns.add($ColText)
#List option.
$DomainUsers = Get-ADUser -Filter *
ForEach($DUsers in $DomainUsers) {
#$WPFDomainUsersBox.Items.Add($DUsers.SamAccountName)
$datarow = $datatable.NewRow()
#Enter data in the row
$datarow.Value = $DUsers.SamAccountName
$datarow.Text = $DUsers.SamAccountName
#Add the row to the datatable
$datatable.Rows.Add($datarow)
}
$WPFDomainUsersBox.Datasource = $datatable
I keep getting the following:
The property 'Datasource' cannot be found on this object. Verify that
the property exists and can be set.
A WPF ComboBox has no property named "Datasource". It has an ItemsSource property that you can set to any IEnumerable like for example the DataView of a DataTable:
$WPFDomainUsersBox.ItemsSource = $datatable.DefaultView
Related
I am trying to pre-sort a column in a DataGrid inside of Powershell. I have created a DataGrid with XAML. I am populating the DataGrid with a DataTable in Powershell.
My current attempt:
$UI.DataGrid.DataContext = $DataContext
$DataContext = $DataTable.DefaultView | Sort-Object -Property 'Property 1'
However, when I run this, my DataGrid is filled with unnecessary columns such as: Row Version, Row, IsNew, IsEdit, and Error. I have been looking for a property or method that allows me to sort in both the DataTable and DataGrid objects. Unfortunately, I have been unable to find either that help me in this situation. I have been reading a bit on DataGridView options. Note: this is not a DataGridView, but a DataGrid.
I'm using ShowUI to explore PowerShell GUIs, it's based on WPF behind the scenes, and I'm trying to get databinding to work. Ideally I'd like a hashtable of my data and textboxes bound to the properties, so that typing in the textboxes updates the hashtable and updating the hashtable updates the textboxes. Or something approximating that.
I don't know my way around WPF databinding, I'm trying things like this:
ipmo showui
StackPanel {
label -name "a" -Content { binding -ElementBinding b -path Text }
Textbox -name "b"
} -Show
and what I get is a UI showing up, but typing doesn't change anything. I've tried quite a few trial-and-error variations on this - setting the binding on the TextBox instead using Textbox -Text { binding... }, or binding on both, using syntax like binding -Source a instead of ElementBinding, using binding -Source $a with the control's variable name, using -DataContexts on the textbox or on the parent stackpanel, trying with and without default values in various places for the commands. I've tried using a button with an event handler that updates a hashtable and trying to bind the hashtable, or with a PSCustomObject; binding a textbox to a slider value - a lot of trial and error, but no result.
There is an example of databinding in ShowUI here which pulls command help into a list and steps through it, that appears to work fine. And the first example here also works fine - as long as you have pictures in the folder. These make me think ShowUI can handle databinding - and without scaffolding or initialization code.
Following this C# tutorial and trying to port it almost literally to ShowUI, I get this:
ipmo showui
$s = [pscustomobject]#{fname="Mahak"; lname="Garg";}
Grid -Name "StuInfo" -rows 3 {
TextBox -Text { Binding fname } -row 0
Textbox -Text { Binding lname } -row 1
Button -name "button1" -Content "Next" -row 2 -On_Click {
$s2 = [pscustomobject]#{fname="Jupi"; lname="Gupta";}
$stuinfo.DataContext = $s2
Write-Host "."
}
} -On_Loaded {
$stuinfo.DataContext = $s
} -show
And the UI appears, and the data does not. Clicking the button writes a . to the console, but does not update the textboxes.
I'm using PowerShell 4 so I can't directly use classes, and the latest ShowUI 1.5 (I think) dev branch from Github. [Edit: this was at least part of my problem, actually using an old version on one computer and a new version on another]
What am I missing or misunderstanding?
I've never used ShowUI before (didn't know it existed until I saw this question). So, I downloaded it and tried your script above. I got the same results as you did. I found that the $s variable in the On_Loaded method was null. Changing the two instances of $s to $global:s made it work:
ipmo showui
$global:s = [pscustomobject]#{fname="Mahak"; lname="Garg";}
Grid -Name "StuInfo" -rows 3 {
TextBox -Text { Binding fname } -row 0
Textbox -Text { Binding lname } -row 1
Button -name "button1" -Content "Next" -row 2 -On_Click {
$s2 = [pscustomobject]#{fname="Jupi"; lname="Gupta";}
$stuinfo.DataContext = $s2
Write-Host "."
}
} -On_Loaded {
$stuinfo.DataContext = $global:s
} -show
The first script you had, I couldn't get binding to an element working. I tried using a DataContext to link them that way and it worked... sort of. It seems like whatever binds to the property first is the only item that can update it. So, if the textbox is first then it worked but if the label was first it didn't work.
Works:
ipmo showui
$dc = [pscustomobject]#{ myText="my text" }
StackPanel {
Textbox -Text {Binding -Path myText -UpdateSourceTrigger PropertyChanged}
Label -Content {Binding -Path myText}
} -Show -DataContext #($dc)
$dc
Didn't work:
ipmo showui
$dc = [pscustomobject]#{ myText="my text" }
StackPanel {
Label -Content {Binding -Path myText}
Textbox -Text {Binding -Path myText -UpdateSourceTrigger PropertyChanged}
} -Show -DataContext #($dc)
$dc
I hope this helps you some... it seems like ShowUI would be a nice tool but IDK, seems buggy.
I am attempting to perform a filter on an Access database with Visual Studio 2010. The user will enter the search criteria into a textbox and I would like the results to be displayed in a listbox with the ability to update other fields depending on the selecteditem from the listbox. The current code I have is:
Dim dv As New DataView
With dv
.Table = subcategoryDataSet.Tables("Subcategory")
.AllowDelete = False
.AllowEdit = False
.AllowNew = False
.RowFilter = "Description LIKE '" & txtSearchBox.Text & "*'"
.Sort = "ID"
End With
'Now I am lost
Not sure if I need to create a datasource from the dv table? Or how to bind it properly to the listbox, or if I even need to bind it to the listbox?
Any help would be greatly appreciated.Thanks!
You should simply set the DataSource of the Listbox to your DataView and set the DisplayMember to the field that you want to see in the Listbox.
Something like this
ListBox1.DataSource = dv
ListBox1.DisplayMember = "Description"
I'm querying an online service (google data feed) which can return results that will have different numbers of columns and rows with each request.
So far I have been unable to get the data grid or grid to work for me. Ideally I want something that works like excel - you can just add rows and set the values for an individual cell
You can create a class e.g. myGridCol that represents the column and create a collection of the columns. Read the google data feed and create the columns. Then you need to add the columns individually e.g. myGridCol[0], myGridCol[1] .. as DataGridColumns in the code behind. You cannot bind directly to a column collection.
You simply bind to a collection for the rows that has a collection for the columns.
In my case I am using a GridView but I have used the same approach with DataGrid
In my case sDocs is an ObservableCollection
sDoc has public List DocFields
The Fields collection is exactly the same in each sDoc because I made sure it was.
If the Fields collection is not the same in each sDoc then it does not like that.
sDocs is the ItemsSource for the GridView
Then in the code behind I add the columns. As I said before you cannot bind directly to a columns collection. Notice you can even bind the Path to a Property (.e.g. DispValueShort). My class for DocField has other Properties and methods. DocField is actually an Abstract Class with and Abstract Property DispValueShort. Then I have classes for string, date, and enumeration that implement DocField because edit of a string is different from edit of a date. I even have classes for single value and multi value. This is a stable production application.
Binding
<ListView Grid.Row="1" Grid.Column="0" x:Name="lvSrchResulsGrid"
ItemsSource="{Binding Path=MyGabeLib.Search.SDocs}"
Code behind
sDocBaseResultDocsFieldsIndex = 0;
foreach (GabeLib.DocField docField in sDocBaseResultDocsFields)
{
// Debug.WriteLine(" sDocBaseResultDocsFields DispName = " + docField.FieldDef.DispName);
if (fd.FieldDef == docField.FieldDefApplied.FieldDef)
{
gvc = new GridViewColumn();
gvch = new GridViewColumnHeader();
gvch.Content = fd.FieldDef.DispName;
gvch.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
if (fd.FieldDef.Sort)
{
gvch.Click += new RoutedEventHandler(SortClick);
gvch.Tag = fd.FieldDef.Name;
}
if (!fd.AppliedDispGrid) gvc.Width = 0; // how to hide
gvc.Header = gvch;
gvBinding = new Binding();
gvBinding.Mode = BindingMode.OneWay;
gvBinding.Path = new PropertyPath("DocFields[" + sDocBaseResultDocsFieldsIndex.ToString() + "].DispValueShort");
template = new DataTemplate();
textblock = new FrameworkElementFactory(typeof(TextBlock));
textblock.SetValue(TextBlock.TextProperty, gvBinding);
textblock.SetValue(TextBlock.TextTrimmingProperty, TextTrimming.WordEllipsis);
// <Setter Property="TextTrimming" Value="WordEllipsis" />
template.VisualTree = new FrameworkElementFactory(typeof(Grid));
template.VisualTree.AppendChild(textblock);
gvc.CellTemplate = template;
gvSearchResults.Columns.Add(gvc);
break;
}
sDocBaseResultDocsFieldsIndex++;
}
I have ListBox and want to put values in this listbox from a DataTable:
listBoxVisibleFields.DataContext = SelectedFields;
Where SelectedFields is a DataTable filled with data. But this code does not work. My ListBox is empty. As I remember, in WinForms was sucha a thing for list box like ValueMember and DisplayMember, but in WPF I dont find something like that...
Does someone know how to fill simply my ListBox from DataTable?
The property you are looking for is ItemsSource instead of DataContext. The property most closely resembling ValueMember is called SelectedValuePath (see this example). The analogon for DisplayMember is called DisplayMemberPath.
EDIT: So, your code should look like this:
DataTable SelectedFields = ...;
listBoxVisibleFields.SelectedValuePath = "myID";
listBoxVisibleFields.DisplayMemberPath = "myTextField";
listBoxVisibleFields.ItemsSource = SelectedFields.DefaultView;
Alternatively, the two path values can be set in XAML
<ListBox ... SelectedValuePath="myID" DisplayMemberPath="myTextField" />
which is a bit more elegant.
Here is my code, hope is usefull
Dim connectionString As String = "Data Source=(local);Database=Catalogos;UID=sa;Password=S1santan"
Dim conSQL As New SqlClient.SqlConnection()
conSQL.ConnectionString = connectionString
conSQL.Open()
Debug.Print("SELECT * FROM CodigoPostal WHERE CP= '" & _Codigo & "'")
Dim adaptSQL As New SqlClient.SqlDataAdapter("SELECT * FROM CodigoPostal WHERE CP= '" & _Codigo & "'", conSQL)
Dim DatosCP As New DataSet
adaptSQL.Fill(DatosCP, "CodigoPostal")
'Here you select the table inside a data set
ListBox1.ItemsSource = DatosCP.Tables("CodigoPostal").DefaultView
'Here select which field to show
ListBox1.DisplayMemberPath = "d_asenta"
'and at last here you select the field you want to use to select values from
ListBox1.SelectedValuePath = "ID"
conSQL.Close()