WPF combobox two way binding with TextBlock - wpf

I created the following list
List<cb> combolist = new List<cb>();
combolist.Add(new cb() { name = "Neo",bloodgroup = "O+ve"});
combolist.Add(new cb() { name = "meo", bloodgroup = "O" });
combolist.Add(new cb() { name = "bsv", bloodgroup = "B+ve" });
cboxnames.ItemsSource = combolist;
Now I am creating a combobox that gets data from the above list using Item template
<ComboBox Margin="12,31,421,258" Name="cboxnames" IsEditable="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Now I am creating an additional textblock that displays the item that is selected in the combobox
<TextBlock Height="28" HorizontalAlignment="Left" Background="LightGray" Margin="0,138,0,0" Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=SelectedItem.Content}" VerticalAlignment="Top" Width="191" />
The problem is whenever I am selecting an item from combobox, that item is not displayed in the textblock, please help!!!

<TextBlock Text="{Binding UpdateSourceTrigger=PropertyChanged,
ElementName=cboxnames,
Mode=TwoWay,Path=SelectedItem.name}"/>
use this..hope it helps you

Hi Check your TextBlock Binding . It should be SelectedItem.name instead of SelectedItem.Content
Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=**SelectedItem.name**}"
I hope this will help.

Related

WPF combobox dynamic binding

I've a combo box with in data grid edititemtemplate and i write some code in combo box loaded event like:
Code:
private void cmbGFld_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
cmb.ItemsSource = FieldsList.GetFieldList();
ConditionField cData = condLists[FieldGrid.SelectedIndex];
cmb.SelectedItem = cData.FieldType;
}
XAML Code:
<toolkit:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:Name="editTemplate">
<ComboBox Loaded="cmbGFld_Loaded" BorderBrush="Transparent" SelectedItem="{Binding Path=FieldType}" SelectedValuePath="Name" BorderThickness="0" FontSize="13" FontStyle="Italic" FontWeight="Normal" Foreground="DimGray" x:Name="cmbGFld" Template="{StaticResource ComboBoxTemplate2}">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Green</SolidColorBrush>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellEditingTemplate>
But the problem is when ever i try to edit the combo box it doesn't showing which is already selected, any one help me.
Thanks,
#nag.
Try it without cmb.SelectedItem = cData.FieldType; in cmbGFld_Loaded(). This will overwrite the binding SelectedItem="{Binding Path=FieldType}" in your XAML. Set the selected item in the binded FieldType property instead. I don't know your application but something like:
FieldType = condLists[FieldGrid.SelectedIndex].FieldType;

Caliburn Micro -- Change Individual Items in BindableCollection

I am displaying a collection in a listview, and each listview item has its own button for downloading the related pdf.
I want to click the button and display an indeterminate progress bar in it's place.
In my sample project, I'm merely trying to invert the item's download status when I click its button.
My viewmodel code:
private BindableCollection<IDownloadable> _downloadables;
public BindableCollection<IDownloadable> Downloadables
{
get { return _downloadables; }
set
{
_downloadables = value;
NotifyOfPropertyChange(() => Downloadables);
}
}
public void Download(IDownloadable item)
{
item.Downloading = !item.Downloading;
NotifyOfPropertyChange(() => Downloadables);
}
public ShellViewModel()
{
Downloadables = new BindableCollection<IDownloadable>();
Downloadables.Add(new DownloadString("String"));
Downloadables.Add(new DownloadString("String"));
Downloadables.Add(new DownloadString("String"));
}
My view:
<ListView Name="Downloadables"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="White">
<ToggleButton Content="{Binding MyString}"
Width="50"
Height="50"
HorizontalAlignment="Center"
VerticalAlignment="Center"
cal:Message.Attach="Download($dataContext)"/>
<spark:SprocketControl Width="30"
Height="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"
AlphaTicksPercentage="50"
Interval="60"
IsIndeterminate="True"
LowestAlpha="50"
StartAngle="-90"
TickColor="LawnGreen"
TickCount="12"
TickWidth="3"
Visibility="{Binding Downloading, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I had imagined that NotifyOfPropertyChanged would tell the collection items to update, but that doesn't appear to be the case.
This has has nothing to do with collection. The template binds to the item itself. It has no idea about the collection. This means, you need to implement notification (INotifyPropertyChanged) in the item itself.

ObservableCollection Images in Listbox to Content Control master detail WPf

I have an observablecollection of Images that get populated via the following code:
<StackPanel Orientation="Horizontal" Grid.Column="0">
<ListBox ItemsSource="{Binding BigImageView}" IsSynchronizedWithCurrentItem="True"
SelectedIndex="0" SelectedItem="{Binding CurrentItem}" />
</StackPanel>
<ContentControl Name="Detail" Content="{Binding BigImageView, Mode=OneWay}"
Margin="9,0,0,0" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
However the Content Control is supposed to bind to the BigImageView via an ObservableCollection
BigImage = new ObservableCollection<Image>();
_listView = CollectionViewSource.GetDefaultView(BigImage);
_listView.CurrentChanged += new EventHandler(OnCurrentChanged);
public System.ComponentModel.ICollectionView BigImageView
{
get
{
return _listView;
}
set
{
_listView = value;
OnPropertyChanged("BigImageView");
}
}
I want to return the image to the content control when I move the listbox. I have been racking my brain and trying everyhitn but it does not work. any help would be appreciated.
There is no need to bind the selecteditem, the collectionview should take care of that.
Try this:
<ListBox ItemsSource="{Binding BigImageView}" IsSynchronizedWithCurrentItem="True" />
<ContentControl Name="Detail" Content="{Binding BigImageView, Mode=OneWay}" VerticalAlignment="Top">
<ContentControl.ContentTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
<ContentControl.ContentTemplate>
1
Create a viewmodel with a list and a selected item:
public class BigImageViewModel : INotifyPropertyChanged
{
private string bigImage;
//string for path?
public ObservableCollection<string> BigImageView {get; set; } //Of course, make sure it has a value
public string SelectedBigImage
{
get { return bigImage; }
set { bigImage = values; NotifyPropertyChanged("SelectedBigImage"); }
}
}
Set this object on the DataContext of your control in the constructor:
DataContext = new BigImage(); //Make sure you initialize your list
Set the ListBox ItemsSource to your BigImage list, bind your SelectedItem to BigImageView
and use that in your content control:
<ListBox ItemsSource="{Binding BigImageView}" SelectedItem={Binding SelectedBigImage} />
ContentControl:
<ContentControl Name="Detail" Content="{Binding SelectedBigImage, Mode=OneWay}" VerticalAlignment="Top">
<ContentControl.ContentTemplate>
<DataTemplate>
<Image Source="{Binding}"/> <!-- Nice template for showing your string BigImage -->
</DataTemplate>
<ContentControl.ContentTemplate>
</ContentControl>
2
Or screw that view model:
Set the list directly in the constructor (after the InitializeComponent() ):
myListBox.ItemsSource = ObservableCollection<string>(); //Make sure you initialize your list with whatever your object is..
Give the list a name:
And bind with an ElementName binding to your selected item:
<ContentControl Name="Detail" Content="{Binding ElementName=myListBox, Path=SelectedItem}" VerticalAlignment="Top">
<ContentControl.ContentTemplate>
<DataTemplate>
<Image Source="{Binding}"/> <!-- Nice template for showing your string BigImage -->
</DataTemplate>
<ContentControl.ContentTemplate>
</ContentControl>

Bind ListBox to Dictionary<int, string>

I would like to bind a Silverlight ListBox to Dictionary<int, string>. I have tried the following without success:
someListBox.ItemsSource = someItems;
and
someListBox.ItemsSource = someItems.Values;
Both of those approaches will work assuming the dictionary is fully populated at the time of the assignment. Given simply this in your user control:-
<ListBox x:Name="lst" />
then this code:-
var data = new Dictionary<int, string>();
data.Add(1, "Hello");
data.Add(2, "World");
lst.ItemsSource = data.Values;
Will display the two strings "Hello" and "World".
Give the ListBox a template:-
<ListBox x:Name="lst">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Key}" Margin="5" />
<TextBlock Text="{Binding Value}" Margin="5" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now you can assign the dictionary itself:-
lst.ItemsSource = data;
The listbox displays the set of Key value pairs.
The solution to this problem was very simple: I was not calling InitializeComponent(); before I attempted to bind data to the control.

In WPF how to bind a collection within the parent control's itemssource

I'm new to WPF so this may be easier than it seems. I have a DataTable object that I set as the itemssource of a combobox. For each row in the DataTable I want a ComboBoxItem. For each ComboBoxItem I want to create a label for every column name and a text box for the corresponding value, in the current row, of that column. Nothing I try seems to work but heres my shot at the datatemplate in XAML.
<Grid x:Name="LayoutRoot" Background="White" Height="107" Width="358">
<ComboBox Name="pCombo" ItemsSource="myTable">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel DataContext="{Binding pCombo.ItemsSource.Columns}">
<TextBlock Text="{Binding ColumnName}"></TextBlock>
</StackPanel>
<StackPanel DataContext="{Binding pCombo.ItemsSource.Rows}">
<TextBox Text="{Binding RowValue}"></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
I know all my Bindings are wrong I just can't figure out what should be there instead. Thanks for anyone that helps me out.
XAML:
<ListView ItemsSource="{Binding Path=Tbl}">
<ListView.ItemTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=Key}"></Label>
<Label Content="{Binding Path=Value}"></Label>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
private object tbl = new[]
{
new[] {
new KeyValuePair<string, string>("col1", "val1"), new KeyValuePair<string, string>("col2", "val1")
},
new[] {
new KeyValuePair<string, string>("col1", "val2"), new KeyValuePair<string, string>("col2", "val2")
},
new[] {
new KeyValuePair<string, string>("col1", "val3"), new KeyValuePair<string, string>("col2", "val3")
}
};
public object Tbl { get { return tbl; } set { tbl = value; } }
Don't forget to set the DataContext (i.e. in the .ctor of the window) like this:
DataContext = this;
I just hope you get the idea behind this!

Resources