Why does Binding within a CellTemplate not work? [closed] - wpf

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to bind an ObservableCollection called "MaterialList" to a Combobox placed in a column.
C#
public class Delivery
{
//Some other Properties which work
...
public ObservableCollection<string> MaterialList { get; set; }
public ObservableCollection<OpenPosition> OpenPositions { get; set; }
public Delivery()
{
this.MaterialList = new ObservableCollection<string>();
this.OpenPositions = new ObservableCollection<OpenPosition>();
}
}
I've checked if there is data in that list, and there is!
XAML:
<dxg:GridControl x:Name="grid4" MaxHeight="2000" AutoGenerateColumns="None" ItemsSource="{Binding SelectedDelivery.OpenPositions}">
<dxg:GridColumn" Header="{Binding LanguageTexts[_MaterialNumber]}">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="ComboBox" ItemsSource="{Binding SelectedDelivery.MaterialList}" SelectedIndex="0" />
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
My ComboBox doesn't display anything. I am guessing there is something wrong because I have use the DataContext from the Itemssource.
Hope you can help me!

Try setting the Binding as ItemsSource="{Binding DataContext.SelectedDelivery.MaterialList, RelativeSource={RelativeSource FindAncestor, AncestoryType={x:Type ItemsControl}}".
The issue is like you think, the datacontext for the combobox is taken from SelectedDelivery.OpenPositions and the way I suggested is using the DataContext of the view, rather than that from the ItemsSource.
Hope it helps!

Related

How do I bind a Listview SelectedItem to a Textbox using the TwoWay mode?

I am very new to WPF and testing some things that I would like to include in an application that I will be working on. I have a 2 row ListView (bound to a textbox) with the names Scott Guthrie and Jon Skeet in it. I am trying to select "Scott Guthrie" in the ListView and have it populate the TextBox. I want to be able to edit the text and tab off and have the ListView updated.
Edit:I removed the code since that really didn't add anything to the question.
Wow, that's really complicated what you've got there.
This can be accomplished in a very simple way. You need a model to represent the programmer, a view model to hold a list of programmers, and simple binding to take care of the rest.
The model:
public sealed class Programmer
{
public string Name { get; set; }
}
Its very simple. An object representing a programmer with a name. We must encapsulate the name within an object because strings are immutable in .NET. If you tried binding against a single string in a list of strings, changes wouldn't propagate.
The collection of programmers is kept in a ViewModel. In this case, I call it ViewModel, because I have no imagination. This view model contains everything that the view binds against. In this case, its the list of programmers.
public sealed class ViewModel
{
public ObservableCollection<Programmer> Programmers { get; private set; }
public ViewModel()
{
Programmers = new ObservableCollection<Programmer>();
}
}
The ViewModel is set as the DataContext of our view. The DataContext flows down the visual tree, and we can bind against it at any point.
public MainWindow()
{
var vm = new ViewModel();
vm.Programmers.Add(new Programmer { Name = "Jon Skeet" });
vm.Programmers.Add(new Programmer { Name = "Scott Guthrie" });
DataContext = vm;
InitializeComponent();
}
You can set the DataContext in any way you want; I'm doing it here for simplicity's sake.
In the UI, I simply bind the ListView against the list of Programmers in the ViewModel (the DataContext, unless otherwise stated, is the root of the binding path). I then bind the TextBox against the SelectedItem of the ListBox. You select a Programmer from the list, which then becomes the SelectedItem, which I can then change the Name of.
<Window
x:Class="Programmers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:t="clr-namespace:Programmers"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox
x:Name="list"
ItemsSource="{Binding Programmers}"
DisplayMemberPath="Name" />
<TextBox
Grid.Column="1"
VerticalAlignment="Top"
Text="{Binding SelectedItem.Name, ElementName=list}" />
</Grid>
</Window>
Simple, once you get the hang of it.
This works (except that you need to validate the textbox since you can enter any text.. a dropdown might be a better choice).
View:
<TabItem x:Name="RightTabPage" Header="RightModel" DataContext="{Binding Right}">
<StackPanel>
<TextBox Text="{Binding SelectedGuru}"/>
<ListView SelectedItem="{Binding SelectedGuru}" ItemsSource="{Binding Gurus}"/>
</StackPanel>
</TabItem>
ViewModel:
public class RightViewModel
{
public RightViewModel()
{
Gurus = new[] {"Scott Guthrie", "Jon Skeet"};
SelectedGuru = Gurus.First();
}
public string SelectedGuru { get; set; }
public IEnumerable<string> Gurus{ get; set; }
}

How to add row in telerik radGridView dynamically?

I am using Telerik RadGridView control. I want to add row in radGridView in runtime.
I use following code:
<telerikGrid:RadGridView x:Name="TopGrid" AutoGenerateColumns="False" >
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewComboBoxColumn DataMemberBinding="{Binding FieldName}" Width="150" Header="Field Name"/>
<telerikGrid:GridViewComboBoxColumn DataMemberBinding="{Binding Operator}" ItemsSource="{Binding OperatorList, Source={StaticResource OperatorModel}}" Width="150" Header="Operator"/>
</telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>
Can anyone help me?
Not sure what you're using as far as a datasource, but generally speaking you'd be binding the RadGridView to some ItemsSource, then the source for the combo box columns would come from there.
For example, with an entity:
public class SomeEntity
{
public string FieldName { get; set; }
public string Operator { get; set; }
}
Binding the RadGridView to the List would automatically generate rows for this, so adding a new entity to that list would generate a new row. More info on how you're binding/populating the grid would help me give a more specific answer. :)

How do you customize columns in silverlight 4 data binding

This is a very basic question. I have a grid, whose data context is bound to a entity framework service. I simply bound the context to service and I can see the data that is getting bound properly. Now, I want to change couple of coulmns to special controls. Like one column has true or false value and that column I want to display a radio button. One column is date value, I want to display date control. How would one go about doing it?
Thanks.
I'm not exactly sure how to do the Radio Button portion of this, but something like this may get you started:
<ListBox x:Name="LayoutRoot" ItemsSource="{Binding Collection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Text}"/>
<CheckBox Content="True" IsChecked="{Binding Checked, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In this case, you would have a checkbox being bound to the boolean value. I'm not exactly sure what you are using for a date control, but you should be able to place that in the stackpanel also and bind it to the dateproperty of your item.
In the above example, 'Collection' is an observable collection of 'MyObject' which is shown below:
MyObject.cs
public class MyObject
{
public string Text { get; set; }
public bool Checked { get; set; }
public bool InverseChecked { get; set; }
public DateTime Date { get; set; }
}
I also understand that you were using a grid, and I'm showing a ListBox. Not sure if this would work for you, but this is how we've approached it in the past.
Hope this helps!

Silverlight 3 ComboBox ItemTemplate binding

I have a simple ComboBox in my Silverlight 3 application. I want to populate it from an ObservableCollection. The list holds a class that has a Name(string), and a Selected(bool) property. The combo box has as many items as I have in the list, but I cannot seem to get the list data to appear.
Any help would be appreciated.
<ComboBox x:Name="cmbCategory" Grid.Column="3">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<CheckBox IsChecked="{Binding Selected}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
...
private class cmbCategoryClass
{
public string Name { get; set; }
public bool Selected { get; set; }
}
private ObservableCollection<cmbCategoryClass> _categories;
....
cmbCategory.DataContext = _categories;
cmbCategory.ItemsSource = _categories;
I can't tell from your code if this is a Codebehind or a ViewModel. I am guessing that you are actually populating the _categories list in code so that it contains at least one cmbCategoryClass object. Try removing the line that sets the DataContext to _categories as your ItemsSource may be looking for a _categories property on the DataContext Check the Output window in Visual Studio when running in debug mode for clues to data binding failures.

Text Will not show for selected Item in ComboBox

I feel really stupid for asking this but I have been thrashing for over 8 hours. How do I get the Selected Item to show its text in my WPF combo box when selected?
Above is an option dialog that allows users to select and configure the available tournament displays. The problem is the selected combo box item shows the UserControl instead of the Display name.
On Window Loaded:
//_displayer is a private member populated using MEF
//[ImportMany(typeof (IDisplayer))]
//private IEnumerable<IDisplayer> _displayers;
DisplayTypeComboBox.ItemsSource = _displayers;
The ComboBox Xaml:
<ComboBox
Name="DisplayTypeComboBox"
Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="1"
IsEditable="False"
SelectionChanged="DisplayTypeComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<ComboBoxItem Content="{Binding DisplayerName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The IDisplayer:
public interface IDisplayer
{
string DisplayDataLocation { get; set; }
string DisplayerName { get; }
string DisplayerDescription { get;}
bool WatcherEnabled { get; }
UserControl View { get; }
string DisplayerImageLeft { get; set; }
string DisplayerImageRight { get; set; }
void Update();
}
I don't even want to think about how many hours I have spent trying to solve what should be a simple problem. Why is it so hard to get your selected text to appear as the selected value? I give up, WPF you have beat me into submission. I changed the control to a list box it takes up more room to display the selectable Items but at least it works.
<ListBox
Name="DisplayTypeComboBox"
Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="1"
SelectionChanged="DisplayType_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding DisplayerName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I encountered the same thing. Took me a while too. :(
You should have used the ItemContainerStyle and not ItemTemplate.
Because ComboBox wraps the internal items with a ComboBoxItem - you basically wrapped the ComboBoxItem with another one.
Check what DisplayerName member actually contains. Most likely it contains the UserControl name instead of the Display name.
Try using a TextBlock to bind to the DisplayerName instead of a ComboboxItem. I believe that when you set the itemsource, the combo control will wrap the items inside comboboxitems controls automatically.
Edit: I misunderstood your question. Try setting the SelectionBoxItemTemplate.

Resources