Wpf ComboboxEdit from binding global list - c#

<dxg:GridColumn.EditTemplate>
<ControlTemplate>
<dxe:ComboBoxEdit
HorizontalContentAlignment="Left"
ItemsSource="{Binding HizmetSaglayiciList}"
SelectedItem="{Binding Hiz_Sag_Id, Mode=TwoWay}"
ValueMember="Hiz_Sag_Id"
IsTextEditable="False"
AllowNullInput="False"
AutoComplete="False"
ImmediatePopup="False"
EditMode="InplaceActive"/>
</ControlTemplate>
</dxg:GridColumn.EditTemplate>
I have global list called HizmetSaglayiciList, but
The combobox does not open when I press the edit button.
I am writing missing any place.

I think this is one of those cases in which the DataContext is not accessible as certain elements (in this case dxg:GridColumn) are not part of visual or logical tree. A solution may be using a Freezable class. Check this Link.

DataContext for the ComboBoxEdit is not the same as for the GridControl, that's why ItemSource binding fails. Assuming your GridControl has a name (let's say it's x:Name="gridTest"), you can simply do the following:
ItemsSource="{Binding DataContext.HizmetSaglayiciList, ElementName=gridTest}"
Actually, you can bind ItemSource to any named element's DataContext.

Related

How to bind dynamically generated TextBoxes to DataGrid values in WPF?

My problem is basically described in the title of this topic.
I have a datagrid which is dynamically filled with various entries:
<DataGrid
x:Name="searchResult"
AutoGenerateColumns="True"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=SearchResult}" />
Next to it is a bunch of TextBoxes, also dynamically generated:
<ItemsControl
x:Name="searchForm"
ItemTemplate="{StaticResource formInputTest}"
ItemsSource="{Binding SearchForm, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
The template I use in the ItemsControl looks basically like this:
<TextBox
Tag="{Binding Index}"
Text="{Binding ElementName=searchResult, Path=SelectedValue.a, UpdateSourceTrigger=PropertyChanged}" />
Now lets asume my datagrid has 2 columns (a and b) and I have 2 TextBoxes with identical names (a and b).
Now I want to select a datagridrow and like the two text boxes to be automatically filled with the values from the corresponding row.
Unfortunately, I don't know how to get this dynamically Binding.
If I bind the TextBox's Text-property statically to searchResult->SelectedValue.a it works fine, but as you can see, only static.
Thats what I've done in the third code-box above.
The ItemsControl, which contains the TextBoxes, has it's binding to "SearchForm". SearchForm has the property "Index", which is the name of the Column.
So, the TextBox knows the name of the column it has to be binded to. To clarify this, I binded the TextBox.Tag to Index, which works fine.
Basically, I want my TextBox-Binding to look like this:
<TextBox
Text="{Binding ElementName=searchResult, Path=SelectedValue.{Binding Index}, UpdateSourceTrigger=PropertyChanged}" />
But, as you might know, a binding inside a binding does not work.
Does anybody know how to bind these to dynamically generated objects together?
Thanks for any hint!
UPDATE 2019/03/07
Thanks dymanoid!
This has got me some progress but I'm still not at the finish line.
I created the view-model property and binded both the Datagrid-SelectedItem and the TextBox-Text property to it.
Since the SelectedItem is a kind of a dictionary (BindablePublicDictionary) I use a converter (IMultiValueConverter) at the TextboxBinding, to convert the dictionary entry to both the index of the textbox and the text/value.
This is working fine, I select a datagrid-row and the 3 dynamically generated inputfields are beeing filled.
Unfortunatelly, if I edit one of these filled textboxes, the other 2 are emptied and my datagrid is unchanged.
I implemented OnPropertyChanged, my bindings are TwoWay and the ConvertBackFunction is called and returns the data in compliance with the targetType Type from the function itself.
Does anybody have experience with the bindings/converter between a datagrid/dictionary and textboxes, especially regarding the convertBack function?

Overwrite ListBoxitem?

I'm new to WPF and I have this ListBox which I want to instantiate with a specific ListBoxItem, so the user knows what to do with the ListBox.
<ListBox Name="DbListBox"
Grid.Column="3"
HorizontalAlignment="Left"
Height="246"
Margin="0,99,0,0"
Grid.Row="1"
VerticalAlignment="Top"
Width="211"
SelectionMode="Single"
SelectedItem="{Binding Path=selectedDB,Mode=TwoWay}"
AllowDrop="True"
Drop="DbListBox_Drop">
<ListBoxItem Name="ListBoxItem" FontStyle="Italic">Drag .db file here or add below</ListBoxItem>
</ListBox>
Then I have some code which adds a collection of items to the ItemsSource of this ListBox, but I can't do that since the ItemsSource is not empty
DbListBox.ItemsSource = DbCollection;
My question is, how can I start up the ListBox with the item inserted first, and then when DbCollection is added to it, it simply overwrites the first ListBoxItem?
When using WPF properly, we'd normally have something like this, where a collection property would be data bound to the ListBox.ItemsSource property:
<ListBox ItemsSource="{Binding SomeCollectionProperty}" />
Once we have this XAML, we don't need to touch the ListBox again, as we can add or remove items from the data bound collection and they will magically appear (or disappear) from the ListBox:
SomeCollectionProperty.Add(new SomeDataType());
SomeCollectionProperty.Remove(someItemFromCollection);
The SomeDataType used here is up to you... it depends on what you want to display in your items. If it was just a plain string for example, then you could simply do this to add your initial item into the collection:
SomeCollectionProperty.Add("Drag .db file here or add below");
If you wanted to make that item look different to the others then you'd need to data bind a custom class that has a Text property and FontStyle property for example. You could data bind these properties in a DataTemplate to design each item to be exactly as you want it. However, that's a completely different question.
To find out more about these things, you can read the Data Binding Overview and Data Templating Overview page on MSDN.

WPF Combobox issue

I have a combo box in a wpf c# application. In the xaml i am trying to do the following.
The ItemsSource comes from one variable.
SelectedItem sets a value on another variable
But i want the text displayed to come from a new variable.
How do i stop making the selected itemssource appear as the main text?
<ComboBox x:Name="ComboPlay" FontSize="14" MinHeight="20" Margin="0,2,4,4" Grid.Row="1" Grid.Column="3" MinWidth="160"
ItemsSource="{Binding ComboBoxList}"
SelectedItem="{Binding OutputChannel.Value, Converter={StaticResource ResourceKey=ValueToStringConverter}}" Grid.ColumnSpan="1"
IsEnabled="{Binding IsDriveChoiceEnabled}"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
If you mean changing the display of the item currently selected (the portion of the control shown when the dropdown is closed), take a look at Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?
Honestly, a quick search dug up that one and many similar ones. Probably the simplest way, like the linked answer, is to figure out if your item is wrapped in a ComboBoxItem and display it differently then. Or you could re-template the ComboBox. Or you could derive from it (and re-template it) and provide a separate dependency property for the template of the selected item, if you expect to reuse it in different contexts. The sky's the limit.

Visible name in XAML

I have a datagrid with a column containing a ComboBox. I've set the Name for my combobox, but this name is not visible in code, why?
<DataGrid ...>
<DataGrid.Columns>
<DataGrid.TemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="mex" Style="{DynamicResource ComboBoxStyle}"
ItemsSource="{Binding Path=combolist}"
SelectionChanged="status_SelectionChanged" Height="auto" Width="Auto">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGrid.TemplateColumn>
...
</DataGrid.Columns>
</DataGrid>
In C# code mex is empty, not visible, why?
I tried x:Name="mex" as well, but it is still not visible.
c#:
mex.ItemsSource = dt;
undifined mex
A DataGridColumn is never actually in the Logical or Visual Tree; it is always a DataGridRow with DataGridCells, since they are automatically created for each row in the DataGrid.
The only way to reach your component is to build a complex Binding or find it using a Logical or Visual Tree helper.
BTW you should set your ItemsSource of your ComboBox through Bindings to available data from your row. You can't create bindings using ElementName within a DataGridTemplateColumn as again it is not in the Logical or Visual Tree.
I found an interesting link that explains the Visual Tree of a DataGrid: http://blogs.msdn.com/b/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx.
You cannot directly reference elements when they are stored within an items template. Normally you'd want to allow the viewmodel to handle the bindings by setting the datacontext to the parent object and then allow the elements within the items template to pick this up. However based upon your question it looks as if you are attempting to do this directly from code behind.
Here are two similar questions and solutions for setting up the datacontext of an item as well as referencing an element within an items template. Hope this helps
Access parent DataContext from DataTemplate
and
WPF - ItemsControl - How do I get find my "CheckBox" item that is in the ItemTemplate?
because template is used by all rows.if name is usable .must have the same name in visual tree at one time.

How to bind a listbox to a collection within a complex type located within my ViewModel?

My view model currently contains a "SelectedClient" property which refers to the selected "Client" object in a datagrid. This selected client property contains a property titled "OfficeLocations" which is essentially just a list of "OfficeLocation" objects.
I am trying to bind a listbox to the SelectedClients.OfficeLocations property like so:
<ListBox ItemsSource="{Binding SelectedClient.OfficeLocations}" />
But for some reason the ListBox always shows up blank. Once again, in debug mode when I view the SelectedClient.OfficeLocations property, it does in fact contain data.
I've also tried something like:
<ListBox ItemsSource="{Binding SelectedClient, Path=OfficeLocations}" />
To no avail.
Any ideas would be greatly appreciated..Thanks!
Ahh, so it turns out that I was trying to do this binding with a DataGrid.RowDetailsTemplate which overrides my DataContext to SelectedItem already...meaning changing my binding to look like:
<ListBox ItemsSource="{Binding OfficeLocations, Mode=TwoWay}">
Fixed the issue!

Categories

Resources