Reputation: 99
Here are my current codes, simplified to the relevant:
<telerik:RadListBox Grid.Row="0" Grid.Column="2" Margin="0, 5, 5, 5"
x:Name="listBoxIssue" HorizontalAlignment="Left" VerticalAlignment="Top"
Height="690" Width="793"
ItemTemplate="{StaticResource lbIssueTemplate}" ItemsSource="{Binding}">
Template:
<DataTemplate x:Key="lbIssueTemplate">
<Grid Margin="0" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ComboBox x:Name="PrintCode" Grid.Row="0" Grid.Column="2" ItemsSource="{Binding}"
SelectedValuePath="PrintCode" DisplayMemberPath="PrintCode" />
</Grid>
</DataTemplate>
So with this binding setup, there should be a value attached to the ComboBox's SelectedPath. Now I just need to bind a list of choices to the ComboBox. I accomplished this in GridView with the following code:
((GridViewComboBoxColumn)gridPrintOption.Columns["PrintCode"]).ItemsSource = listPrintCode;
I am hoping to achieve something like the code above in this scenario as well. The difficulty here is that I can't get the name of the Column nor can I use .Columns since these are DataTemplate.
Note: I'm not using MVVM.
Upvotes: 1
Views: 432
Reputation: 12295
try this
xaml
<Window.Resources>
<DataTemplate x:Key="lbIssueTemplate">
<Grid Margin="0" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ComboBox x:Name="PrintCode" Grid.Row="0" Grid.Column="2"
SelectedValue="{Binding DataContext.PrintCode, RelativeSource={RelativeSource Mode=Self}, Mode=TwoWay}"
ItemsSource="{Binding DataContext.Choices, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox Background="Red" ItemTemplate="{StaticResource lbIssueTemplate}" ItemsSource="{Binding Equips}"></ListBox>
</StackPanel>
xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
Equip class I have used only PrintCode property
public class Equip:INotifyPropertyChanged
{
public Equip()
{
PrintCode = "All";
}
private string printcode;
public string PrintCode
{
get { return printcode; }
set { printcode = value; OnPropertychanged("PrintCode"); }
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertychanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
ViewModel
public class ViewModel
{
public ViewModel()
{
Choices = new ObservableCollection<string> { "All", "Left", "Right", "None" };
Equips = new ObservableCollection<Equip>() { new Equip(), new Equip() };
}
public ObservableCollection<Equip> Equips { get; set; }
public ObservableCollection<string> Choices { get; set; }
}
Upvotes: 1