set a hidden link on a textblock with Binding [duplicate] - c#

For now I am displaying in one of the columns in listView, GridView one of the property of my class. This looks more or less:
<ListView x:Name="offers_listView" Margin="38,185,35,81" >
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="itemId" DisplayMemberBinding="{Binding Path=itemId}" />
ItemID is a number.
What I would like to do is make a hyperlink based on this number. For example from ItemId equals to 1234 I would like to make clickable link to address www.website.com/showItem.php?itemId=1234. The best solution would be if the column still display 1234 but it is clickable link to the address I mentioned. Of course for the whole list, each item have different itemID property.
May anyone give me a hint how to do it or sample of code I can based ?
edit:
Solution gave below have been adapted by me.
However on the logic side NavigateUri have been null, so the link opened in browser have been not correct. I have adapt this idea directly to textblock as I paste my code below:
<GridViewColumn Width="100" Header="itemLink">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=itemId}" MouseDown="TextBlock_MouseDown"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
var urlPart2 = ((TextBlock)sender).Text;
//var urlPart = ((Hyperlink)sender).NavigateUri;
var fullUrl = string.Format("http://allegro.pl/show_item.php?item={0}", urlPart2);
Process.Start(new ProcessStartInfo(fullUrl));
e.Handled = true;
}

You need to change the cell template for the GridViewColumn in order to have the column contain hyperlinks.
For example, the following:
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink NavigateUri="{Binding Path=itemId}" RequestNavigate="Hyperlink_OnRequestNavigate">
<TextBlock Text="{Binding Path=itemId}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
will have the given column filled with hyperlinks. WPF doesn't do anything with hyperlinks by default, so you have to add a handler yourself that opens the link:
private void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
var urlPart = ((Hyperlink)sender).NavigateUri;
var fullUrl = string.Format("http://www.website.com/showItem.php?itemId={0}", urlPart);
Process.Start(new ProcessStartInfo(fullUrl));
e.Handled = true;
}
Of course, change the url to do whatever you want to do.

I have created a reusable command for situations when application has to open a webpage in browser:
public class BrowseCommand : ICommand
{
public virtual void Execute(object parameter)
{
var path = parameter as string;
if (String.IsNullOrWhiteSpace(path) == false)
{
// if path is URL, start browser with selected page
Process.Start(new ProcessStartInfo(path));
}
}
public virtual bool CanExecute(object parameter)
{
var path = parameter as string;
return !String.IsNullOrWhiteSpace(path);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
protected virtual void OnCanExecuteChanged(EventArgs e)
{
CommandManager.InvalidateRequerySuggested();
}
}
BrowseCommand requires URL as parameter. I suggest to extend view model and declare url property (based on Id) there, e.g.
public class ItemVm
{
public int ItemId { get; set; }
public string ItemUrl
{
get { return String.Format("http://stackoverflow.com/questions/{0}", ItemId); }
}
}
the final step is to customize view. after adding BrowseCommand to Resources, I modified CellTemplate (text with underline) and attached middle mouse click command:
<ListView x:Name="offers_listView" Grid.Row="1" ItemsSource="{Binding Path=List}">
<ListView.Resources>
<local:BrowseCommand x:Key="BrowseCmd"/>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="ItemId">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ItemId}"
Cursor="Hand" TextDecorations="Underline">
<TextBlock.InputBindings>
<MouseBinding Gesture="MiddleClick"
Command="{StaticResource BrowseCmd}"
CommandParameter="{Binding Path=ItemUrl}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

Related

Binding a value to a Radiobutton and Checkbox in a ListView

I have a ListView which have a column containing a radiobuttons and a column containing checkboxes. What I would like to do is bind this radiobutton to a value based on a int value on a property.
My ListView look like this:
<ListView x:Name="lvSalesmen" ItemsSource="{Binding}" Margin="311,32,0,244" Grid.ColumnSpan="5">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=Id}"/>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Header="IsResponsible">
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="IsResponsible" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="IsSecondary">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
and this is where I set my DataContext in the code-behind file:
private void Vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
lvSalesmen.DataContext = vm.Salesmen;
}
My ViewModel look like this:
public class DistrictsListViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private List<Salesman> _salesman;
private ISalesmenService _salesmenService = new SalesmenService();
public DistrictsListViewModel()
{
}
public async void GetAllSalesmenWithResponsibilityByDistrictId( int id)
{
Salesmen = await _salesmenService.GetSalesmanReponsibilityByDistrictIdAsync(id);
}
public List<Salesman> Salesmen {
get { return _salesman; }
set {
_salesman = value;
PropertyChanged(this, new PropertyChangedEventArgs("Salesmen"));
}
}
Salesmen is a List where the Salesman object looks like this:
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int IsResponsible { get; set; }
public int IsSecondary { get; set; }
What I would like to achieve is to check the radiobutton in the row where the 'IsResponsible'-property value equals 1. Only one radiobutton can be checked at a time. This I have achieved by adding the 'GroupName'.
Furthermore, the checkbox should be checked in the 'IsSecondary'-column, if the IsSecondary-property value equals 1. Multiple checkboxes can be checked at the same time.
You have a general type mismatch problem. IsChecked is generally a bool value but there are only int values available.
There are two ways to work this out:
You could change the type of IsResponsible and IsSecondary to bool in GetSalesmanReponsibilityByDistrictIdAsync
You can add another property to bind to which internally converty the value of IsResponsible and IsSecondary to bool in the Getoperation
With either one of these suggestions you can then bind the given value to IsChecked in you WPF like:
<RadioButton IsChecked="{Binding IsResponsibleBool, Mode=OneWay}" GroupName="IsResponsible" />
<CheckBox IsChecked="{Binding IsResponsibleBool, Mode=OneWay}"/>
Possbile solution for suggestion #2:
public Boolean IsResponsibleBool
{
get => this.IsResponsbile == 1;
}
public Boolean IsSecondaryBool
{
get => this.IsSecondary == 1;
}

How to look for specific ListViewItem in a ListView - C# (WPF)

I have created ListView (C#-WPF) with three columns: Number, Action and File. First of all I'm adding several different Items to the ListView, the 'Number' is increasing with each entry added, the 'Action' is recording what exactly happened (Moved/Renamed/Removed) and the 'File' column displays for which file an action occurred.
XAML:
<ListView x:Name="ActionFile" HorizontalAlignment="center" Height="100" VerticalAlignment="bottom" Width="780" Margin="20,0,0,0">
<ListView.View>
<GridView>
<GridViewColumn Header="Number" Width="40" DisplayMemberBinding="{Binding NumberX}"/>
<GridViewColumn Header="Action" Width="200" DisplayMemberBinding="{Binding ActionX}"/>
<GridViewColumn Header="File" Width="350" DisplayMemberBinding="{Binding FileX}"/>
</GridView>
</ListView.View>
</ListView>
C#:
public class FileActionEntry
{
public int NumberX { get; set; }
public string ActionX { get; set; }
public string FileX { get; set; }
}
ActionFile.Items.Add(new FileActionEntry() { NumberX = numValue, ActionX = actionValue, FileX = fileValue });
Now, I was trying to create a foreach loop that would check whether a specific action was taken for a specific file, then to clear that entry from a ListView and return its 'Number' value. I had several different approaches but I couldn't find out how to extract a column values out of an item. I thought it could be done by using '.SubItems' but it seems that it does not work for a WPF.
The best way of doing so is to consider binding your listView ItemSource to an ObservableCollection, implement the INotifyPropertyChanged Interface, and any update to that collection will be automatically reflected on the UI.
Consider the following example based on yours, where the entry which the fileName is provided in the TextBox is removed from the collection:
Xaml
<Grid>
<Grid VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Content="Process" Click="ButtonBase_OnClick"/>
<TextBox Grid.Column="1" Margin="2" Text="{Binding SearchText, Mode=TwoWay}"/>
</Grid>
<ListView x:Name="ActionFile" HorizontalAlignment="center" Height="100" VerticalAlignment="bottom" Width="780" Margin="20,0,0,0" ItemsSource="{Binding FileActionEntryCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Number" Width="40" DisplayMemberBinding="{Binding NumberX}"/>
<GridViewColumn Header="Action" Width="200" DisplayMemberBinding="{Binding ActionX}"/>
<GridViewColumn Header="File" Width="350" DisplayMemberBinding="{Binding FileX}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
Code behind
public class FileActionEntry
{
public int NumberX { get; set; }
public string ActionX { get; set; }
public string FileX { get; set; }
}
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _searchText = "";
public string SearchText
{
get
{
return _searchText;
}
set
{
if (_searchText == value)
{
return;
}
_searchText = value;
OnPropertyChanged();
}
}
private ObservableCollection<FileActionEntry> _fileActionEntryCollection = new ObservableCollection<FileActionEntry>()
{
new FileActionEntry(){ ActionX = "Moved", FileX = "File1", NumberX = 1},
new FileActionEntry(){ ActionX = "Renamed", FileX = "File2", NumberX = 2},
new FileActionEntry(){ ActionX = "Removed", FileX = "File3", NumberX = 3}
};
public ObservableCollection<FileActionEntry> FileActionEntryCollection
{
get
{
return _fileActionEntryCollection;
}
set
{
if (_fileActionEntryCollection == value)
{
return;
}
_fileActionEntryCollection = value;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (FileActionEntryCollection.Any(f => f.FileX == SearchText))
FileActionEntryCollection.Remove(FileActionEntryCollection.First(f => f.FileX == SearchText));
}
}
You should (must) use data binding (Data Binding Overview in WPF). This will make your life a lot easier. An essential class in the context of data binding is the ObservableColllection. This collection will notify the binding target about any changes (e.g., add or remove). All ItemsControl like the ListView listen to this changes and will update their view to reflect those changes. So you never need to access the control directly to add or remove items.
The view model is the binding source:
class ViewModel : INotifyPropertyChanged
{
// Ctor
public ViewModel() => this.FileActionEntries = new ObservableCollection<FileActionEntry>();
private void AddFileActionToListView()
{
var newFileEntry = new FileActionEntry() { NumberX = numValue, ActionX = actionValue, FileX = fileValue };
// Add an item to the ListView
// or any other control that binds to FileActionEntries (ObservableCollection)
this.FileActionEntries.Add(newFileEntry);
}
private int CheckFileAction(string action, string file)
{
// Throws an exception if file not found
FileActionEntry fileEntry = this.FileActionEntries.First(entry => entry.FileX.Equals(file, StringComparison.OrdinalIgnoreCase));
// TODO: Check action
// Remove an item from the ListView
// or any other control that binds to FileActionEntries (ObservableCollection)
this.FileActionEntries.Remove(fileEntry);
return fileEntry.NumberX;
}
private ObservableCollection<FileActionEntry> fileActionEntries;
public ObservableCollection<FileActionEntry> FileActionEntries
{
get => this.fileActionEntries;
set
{
this.fileActionEntries = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML with a bound ItemsSource:
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<ListView x:Name="ActionFile"
ItemsSource="{Binding FileActionEntries}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Number" Width="40" DisplayMemberBinding="{Binding NumberX}"/>
<GridViewColumn Header="Action" Width="200" DisplayMemberBinding="{Binding ActionX}"/>
<GridViewColumn Header="File" Width="350" DisplayMemberBinding="{Binding FileX}"/>
</GridView>
</ListView.View>
</ListView>
</Window>

Modify text in column cells and make it hyperlink, listView WPF C#

For now I am displaying in one of the columns in listView, GridView one of the property of my class. This looks more or less:
<ListView x:Name="offers_listView" Margin="38,185,35,81" >
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="itemId" DisplayMemberBinding="{Binding Path=itemId}" />
ItemID is a number.
What I would like to do is make a hyperlink based on this number. For example from ItemId equals to 1234 I would like to make clickable link to address www.website.com/showItem.php?itemId=1234. The best solution would be if the column still display 1234 but it is clickable link to the address I mentioned. Of course for the whole list, each item have different itemID property.
May anyone give me a hint how to do it or sample of code I can based ?
edit:
Solution gave below have been adapted by me.
However on the logic side NavigateUri have been null, so the link opened in browser have been not correct. I have adapt this idea directly to textblock as I paste my code below:
<GridViewColumn Width="100" Header="itemLink">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=itemId}" MouseDown="TextBlock_MouseDown"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
var urlPart2 = ((TextBlock)sender).Text;
//var urlPart = ((Hyperlink)sender).NavigateUri;
var fullUrl = string.Format("http://allegro.pl/show_item.php?item={0}", urlPart2);
Process.Start(new ProcessStartInfo(fullUrl));
e.Handled = true;
}
You need to change the cell template for the GridViewColumn in order to have the column contain hyperlinks.
For example, the following:
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink NavigateUri="{Binding Path=itemId}" RequestNavigate="Hyperlink_OnRequestNavigate">
<TextBlock Text="{Binding Path=itemId}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
will have the given column filled with hyperlinks. WPF doesn't do anything with hyperlinks by default, so you have to add a handler yourself that opens the link:
private void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
var urlPart = ((Hyperlink)sender).NavigateUri;
var fullUrl = string.Format("http://www.website.com/showItem.php?itemId={0}", urlPart);
Process.Start(new ProcessStartInfo(fullUrl));
e.Handled = true;
}
Of course, change the url to do whatever you want to do.
I have created a reusable command for situations when application has to open a webpage in browser:
public class BrowseCommand : ICommand
{
public virtual void Execute(object parameter)
{
var path = parameter as string;
if (String.IsNullOrWhiteSpace(path) == false)
{
// if path is URL, start browser with selected page
Process.Start(new ProcessStartInfo(path));
}
}
public virtual bool CanExecute(object parameter)
{
var path = parameter as string;
return !String.IsNullOrWhiteSpace(path);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
protected virtual void OnCanExecuteChanged(EventArgs e)
{
CommandManager.InvalidateRequerySuggested();
}
}
BrowseCommand requires URL as parameter. I suggest to extend view model and declare url property (based on Id) there, e.g.
public class ItemVm
{
public int ItemId { get; set; }
public string ItemUrl
{
get { return String.Format("http://stackoverflow.com/questions/{0}", ItemId); }
}
}
the final step is to customize view. after adding BrowseCommand to Resources, I modified CellTemplate (text with underline) and attached middle mouse click command:
<ListView x:Name="offers_listView" Grid.Row="1" ItemsSource="{Binding Path=List}">
<ListView.Resources>
<local:BrowseCommand x:Key="BrowseCmd"/>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="ItemId">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ItemId}"
Cursor="Hand" TextDecorations="Underline">
<TextBlock.InputBindings>
<MouseBinding Gesture="MiddleClick"
Command="{StaticResource BrowseCmd}"
CommandParameter="{Binding Path=ItemUrl}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

Get selected Value in ListView c#

I just want to get the selected item in my ListView.
This the XAML:
<TabItem Header="Musique" Background="#1874CD" BorderBrush="#68838B">
<ListView x:Name="ListM" Width="Auto" Background="#D1EEEE" ItemsSource="{Binding Path=MediaCollection}" Margin="-8,-0.877,-1,-2.925" SelectionChanged="ListM_SelectionChanged">
<ListView.View>
<GridView AllowsColumnReorder="True" >
<GridViewColumn Header="" Width="Auto" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Name="IconImage" Source="{Binding IconUri}" Panel.ZIndex="2" Width="15" Height="15"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Header="Titre" Width="auto" />
<GridViewColumn DisplayMemberBinding="{Binding Composer}" Header="Artiste" Width="70" />
<GridViewColumn DisplayMemberBinding="{Binding Length}" Header="Durée" Width="50" />
<GridViewColumn DisplayMemberBinding="{Binding Album}" Header="Album" Width="70" />
</GridView>
</ListView.View>
</ListView>
I insert item with this piece of code :
ListM.Items.Add(new ListGrid() { IconUri = imagemp3.Source, Title = Ftitle, Length = duration, Album = Falbum, Composer = Fcomposer });
I need to get these details (Title, Length, ...) when I select an item on my list. I've tried but had many issues and it still doesn't work.
Using SelectedItems :
SelectedItems is the set of selected rows,
If you want to get only the Title:
string age = ListM.SelectedItems[0].SubItems[0].Text;
If you want to get all the details :
ListM.SelectedItems[0] returns an object. You first need to cast it to its specific type before you can access its members.
Create a Media class (Title, Composer...), then :
private void getSelectedIteminListView(object sender, MouseButtonEventArgs e)
{
Media media = (Media)ListM.SelectedItems[0];
}
Don't forget to add the event :
ListM.DoubleClick += getSelectedIteminListView;
Just cast the SelectedItem to your type:
private void ListM_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listgrid = (ListGrid)ListM.SelectedItem;
}
But it seems you are mixing something. You are using DataBinding (ItemsSource) but you also manually add items.
Better you only use DataBinding by adding the items to MediaCollection in your ViewModel and bind the SelectedItem to another property in the ViewModel:
in XAML:
SelectedItem="{Binding SelectedItem}"
I finaly listen to #M.REJEB
private void ListM_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListGrid media = (ListGrid)ListM.SelectedItems[0];
string age = media.Title;
MessageBox.Show("Selected : " + age);
}
And it works with this class :
class ListGrid
{
public string Path { get; set; }
public string Title { get; set; }
public System.TimeSpan Length { get; set; }
public string Album { get; set; }
public string Composer { get; set; }
public System.Windows.Media.ImageSource IconUri { get; set; }
}

Binding to a WPF combo box in a list view (2-way)

I have this problem of having to bind the selected value of a combo box embedded inside a list view. I have no trouble in displaying items in the combo box. However, I wish I had a way to dictate what the combo box should display (from among the items that it holds) on the click of a button. I think there are several posts on this issue, however, I am not able to get exactly what I want. Here is my code.
XAML:
<Grid>
<StackPanel Orientation="Vertical">
<ListView
x:Name="OptionsListView"
ItemsSource="{Binding MyObjectCollection}">
<ListView.Resources>
<DataTemplate x:Key="comboBoxTemplate">
<ComboBox
Margin="0,3"
x:Name="MyTypeComboBox"
SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}">
<ComboBoxItem Content="ABC"/>
<ComboBoxItem Content="DEF"/>
<ComboBoxItem Content="XYZ"/>
</ComboBox>
</DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Text-Sample"
Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Combo-Sample"
Width="100"
CellTemplate="{StaticResource comboBoxTemplate}" />
</GridView>
</ListView.View>
</ListView>
<Button Click="Button_Click">Click Me!</Button>
</StackPanel>
</Grid>
C# Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
OptionsListView.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//Something here that dictates what should be displayed in the combo box
}
List<MyObject> myObjectCollection = new List<MyObject>();
public List<MyObject> MyObjectCollection
{
get
{
myObjectCollection.Add(new MyObject("One"));
myObjectCollection.Add(new MyObject("Two"));
return myObjectCollection;
}
}
}
public class MyObject : INotifyPropertyChanged
{
private string _name;
public MyObject(string name)
{
// TODO: Complete member initialization
this._name = name;
}
public string Name
{
get
{
return _name;
}
}
string selectedType = string.Empty;
public string SelectedType
{
get
{
return selectedType;
}
set
{
selectedType = value;
this.NotifyChange("SelectedType");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChange(params object[] properties)
{
if (PropertyChanged != null)
{
foreach (string p in properties)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
}
#endregion
}
I would be glad if someone could help me crack this..
Thanks
Ram
I'm not sure if I misunderstanding your question. I think your issue is about the reference issue. I changed your code a little and it works when click on the button.
See the code below.
XAML:
<ComboBox Margin="0,3"
x:Name="MyTypeComboBox"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=DataContext.Sources}"
SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}">
</ComboBox>
C# code:
private Collection<string> sources = new Collection<string>() { "ABC", "DEF", "XYZ" };
public Collection<string> Sources { get { return sources; } }
private void Button_Click(object sender, RoutedEventArgs e)
{
myObjectCollection[0].SelectedType = Sources[0];
myObjectCollection[1].SelectedType = Sources[2];
}
How about
foreach (ComboBox c in OptionsListView.Items)
{
c.SelectedValue = "Put your value here";
}
This should do the work, if you have other objects than comboboxes inside you can add a
if (c is ComboBox)
to ensure that you are working on the right object

Categories