|
Make sure to do not add more than one item with the same identifier or key
|
|
|
|
|
I have a WPF application which contains a ListView defined to show the values for an item in a chart.
One of the fields, State, is supposed to change as the charted values cross warning and alarm barriers.
I built up the list (which was really only one item) as follows:
ObservableCollection<LegendValues> itemList = new ObservableCollection<LegendValues>();
LegendValues item = new LegenValues();
... define the initial values ...
itemList.Add( item );
myListView.ItemsSource = itemList;
item.State = "Warn";
I also have the binding set in XAML as TwoWay.
When I change the state in the item object, it does not change in the ListView. Is there something I'm missing?
Thanks,
Michael
|
|
|
|
|
Michael Eber wrote: Is there something I'm missing?
INotifyProperty perhaps?
|
|
|
|
|
Well...according to the documentation all I need are two things. TwoWay link on the binding between the ItemSource object and the ListIndex, and the ObservableCollection. But I can add INotifyProperty and see if that helps.
|
|
|
|
|
ObservableCollection is only used to tell the binding engine when records have been added or removed from the collection. In order to watch changes to particular properties, you need to use INotifyPropertyChanged and raise the PropertyChanged event.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Have a read up on MVVM, in particular anything to do with the ViewModel portion of that design pattern and as Pete said it's all about the object INSIDE your ObservableCollection.
|
|
|
|
|
try to do add Updatesourcetrigger = Propertychanged to the xaml in addition to the two way, make sure that the object source bound property implement the INotifyPopertyChanged or is a dependency property
|
|
|
|
|
I am having an issue and have scoured the net looking for an answer and I still have yet to find one so I thought I would post here while continuing my search. I simply want to set SelectedItem to a string value and have it select that item from the combobox. Now I am adding my items to the combobox statically during design time and when I do this, SelectedItem doesn't work and always is null. However I just found out that when I add my combobox items during runtime, selectedItem works just fine. Now I would like to know why this is. I am guessing that it is some binding that automatically happens when adding them dynamically, but I would love to know how to get this working statically. Thanks!
|
|
|
|
|
Please specify which version of the framework you are using. Assuming that this is a WPF application.
|
|
|
|
|
Sorry this is WPF and I am working in VS2010 .Net 4.0.
|
|
|
|
|
hello
newbie to WPF, how do you set selected item for a list box?
<br />
SomeDataTab.Columns.Add("SomeData", typeof(string));<br />
SomeDataTab.Columns.Add("IsSelected", typeof(bool));<br />
<br />
Cmd = new SqlCommand(SQL, (SqlConnection)DevConn);<br />
...<br />
Rdr = Cmd.ExecuteReader();<br />
while (Rdr.Read())<br />
{<br />
rw = SomeDataTab.NewRow();<br />
SomeData = (string)Rdr["SomeData"];<br />
if (SomeData == "TEMPLATE_OBJECTIVE")<br />
{<br />
SomeData = "SharedData";<br />
rw["IsSelected"] = true;<br />
}<br />
else<br />
{<br />
rw["IsSelected"] = false;<br />
}<br />
rw["SomeData"] = SomeData;<br />
SomeDataTab.Rows.Add(rw);<br />
}<br />
<br />
SomlstSomeData.DataContext = SomeDataTab.DefaultView;<br />
The above code shows how we bind to DataTable and following is xaml code:
<ListBox Name="lstSomeData" Grid.Row="0" Grid.Column="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
<Label Content="{Binding SomeData}"></Label>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
I wish to know how to pre-select say an Item with value "AAA"?
Thanks!
dev
modified on Sunday, July 4, 2010 9:47 PM
|
|
|
|
|
Is that you require something like this
[Code]<Grid>
<ComboBox Margin="37,31,299,251">
<ComboBoxItem IsSelected="True" Content="One"></ComboBoxItem>
<ComboBoxItem Content="Two"></ComboBoxItem>
<ComboBoxItem Content="Three"></ComboBoxItem>
</ComboBox>
</Grid>[/Code]
This code will show One item as selected by default at the compile time and not after firing the application
|
|
|
|
|
thanks but no, the list is built dynamically by binding to it a DataTable as shown in my code fragment
dev
|
|
|
|
|
You can do this progammatically by using the IsSelected = true of the given MenuItem in the code behid too if I well understand the question, I mean you can test the value AAA using the selected value property of the combo box and then set the IsSelected property of the corresponding item to true
|
|
|
|
|
Thanks no not really... In another post I've added more detail - basically (copied from there), I'm using CodePlex WPF ComboBox and is having trouble pre-select item
From code behind, I bind the combo box to a DataTable with two columns:
...
SomeTableSrc = new DataTable("SomeData");
SomeTableSrc.Columns.Add("Data", typeof(string));
SomeTableSrc.Columns.Add("IsSelected", typeof(bool));
lstSomeData.DataContext = SomeTableSrc.DefaultView;
Xaml:
<Style TargetType="{x:Type ComboBoxItem}">
</Style>
The above will select relevant "ComboBoxItem" (and it works fine, if you click on ComboBox, as menu expands you can see desired items actually get selected), *** BUT *** "SelectedValue" or "Text" of "ComboBox" (not items) remains blank - and setting these "ComboBox" attributes directly just don't work.
// Don't work, "ComboBox" text remains blank
lstSomeData.SelectedValue = "ABC";
// Don't work, "ComboBox" text remains blank
lstSomeData.Text = "ABC";
dev
modified on Wednesday, July 7, 2010 10:09 PM
|
|
|
|
|
Be gentle - this is all new to me!
OK - I have the below sitting on a user control - four progress bars and a text box.
I want the progress bars to each size to the width of the stackpanel - as you can see I have tried 4 different methods - only one of which comes close to doing what I want!
progressBar1 and 3 both appear tiny - despite the width being bound. I have noticed that changing the HorizontalAlignment to 'Stretch' changes that behaviour - but I'm confused!
progressBar2 stretches off the canvas and is clipped. Fair enough, I guess, it's the same width as the panel but starts somewhat to the right, so runs over to the right.
progressBar4 is a mystery to me. It's bound to the ActualWidth of textbox1 - which is pretty thin yet the progress bar appears quite wide - apparently arbitrarily wide?
What gives?
What I was actually trying to do was to have a couple of progress bars, one above the other, 'docked' so that they stretched width-ways with a resizing of the user control.
I suspect that the solution is obvious - so I'll make my excuses now. It's been a long week, and it's beer-o-clock - but what I'm really after is not only a solution, but a pointer to some sort of explanation as to what is going on in the layout.
<StackPanel x:Name="p1" Margin="10">
<ProgressBar Margin="12" Name="progressBar1" Height="18" Width="{Binding ElementName=p1, Path=Width}" HorizontalAlignment="Left" />
<ProgressBar Margin="12" Name="progressBar2" Height="18" Width="{Binding ElementName=p1, Path=ActualWidth}" HorizontalAlignment="Left" />
<ProgressBar Margin="12" Name="progressBar3" Height="18" Width="{Binding ElementName=textBox1, Path=Width}" HorizontalAlignment="Left" />
<ProgressBar Margin="12" Name="progressBar4" Height="18" Width="{Binding ElementName=textBox1, Path=ActualWidth}" HorizontalAlignment="Left" />
<TextBox Height="18" Name="textBox1" Margin="12" HorizontalAlignment="Left" />
</StackPanel>
Thanks, in anticipation of enlightenment,
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
Right - an explanation as to what's going wrong:
<ProgressBar Margin="12" Name="progressBar1" Height="18" Width="{Binding ElementName=p1, Path=Width}" HorizontalAlignment="Left" /> Drop the HorizontalAlignment, that's overriding the Width.
Secondly - this binds to the ActualWidth, but the Margin has an effect, so the actually means that the progress bar extends beyond the margin of the stack panel. Drop the Width setting altogether as it's not needed.
<ProgressBar Margin="12" Name="progressBar2" Height="18" Width="{Binding ElementName=p1, Path=ActualWidth}" HorizontalAlignment="Left" /> If you change all this to the following, you get to see the actual effect of what you are doing:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="p1" Margin="10">
<ProgressBar Margin="12" Name="progressBar1" Height="18" Width="{Binding ElementName=p1, Path=Width}"/>
<ProgressBar Margin="12" Name="progressBar2" Height="18" />
<ProgressBar Margin="12" Name="progressBar3" Height="18" Width="{Binding ElementName=textBox1, Path=Width}" />
<ProgressBar Margin="12" Name="progressBar4" Height="18" Width="{Binding ElementName=textBox1, Path=ActualWidth}" />
<TextBox Height="18" Name="textBox1" Margin="12" HorizontalAlignment="Left" Width="20" />
</StackPanel>
</Page>
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks Pete,
this has now lead to one of those rare occasions where I can't wait util Monday and getting back to work. I would swear I had removed the HorizontalAlignment previously and the progress bars still overran the right margin of the panel... but it was late in the day!
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
You're welcome.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I've got two groups in my datagrid. First one is the main group and the second is the subgroup, what I want to do is hide the Main group and just show the subgroups in my data grid. Here is the code:
ListCollectionView collection = new ListCollectionView(bdata);
collection.GroupDescriptions.Add(new PropertyGroupDescription("tid"));
collection.GroupDescriptions.Add(new PropertyGroupDescription("PkgName"));
dataGrid1.ItemsSource = collection;
Any help would be great.
Thanks
Salman
|
|
|
|
|
Hi,
In my silverlight page i took silver light control : MenuControl 's MenuBar and a image. At run time the menubar's list is hiding back to the image, how to make it view the menubarlist forward of the image at run time.Please suggest me .
With Regards,
CH.Gayatri
|
|
|
|
|
In your xaml, set the canvas.zindex property to a higher value (than the image) for the menubarlist control.
|
|
|
|
|
Hi all,
I have a canvas ( naming cn ) with MouseButtonDown event, i also have three textblocks in it.
When i press a textblock in canvas, the event is raised with the sender is canvas ( of course ).
How could i get the name of clicked-textblock ?
|
|
|
|
|
Check out the original source property of the event and see if it contains that text block.
For more information, see here[^].
|
|
|
|
|
I solve it.
Thank you for your support !
|
|
|
|