wpf binding to static resource - wpf

So I have aproblem with binding in WPF. I am trying to bind a DataGrid ComboBoxColumn with a static resource but with no luck. I know where the problem is but I'm not sure how to fix it.
in XAML i have this:
<local:MyClasificators x:Key="clList"></local:MyClasificators>
and DataGridComboBoxColumn
<DataTemplate>
<ComboBox ItemsSource="{StaticResource clList}" DisplayMemberPath="Value" ></ComboBox>
</DataTemplate>
code for the source I'm binding:
public class MyClasificators:List<KeyValuePair<object, object>>
{
public void _MyClasificators(DataTable country)
{
foreach (DataRow row in country.Rows)
{
this.Add(new KeyValuePair<object, object>(row.ItemArray[0], row.ItemArray[1]));
}
}
And the code for passing the DataTable:
public void callMyClassificators(DataTable country)
{
MyClasificators clasif = new MyClasificators();
clasif._MyClasificators(country);
}
I know that most probably I just have to edit the Resource part, but I'm not sure how should I go about it?

<local:MyClasificators x:Key="clList"></local:MyClasificators>
translates to something like:
Resources.Add("clList", new MyClasificators());
That's it, there's no data in your object.
You could create the resource clList from code, for example in app.xaml.cs:
var countryTable = ... // Get or create table here
var clList = new MyClasificators();
var clList.callMyClassificators(countryTable);
Resources.Add("clList", clList);

From above code, it seems that an instance of MyClasificators is created in resource section. But MyClasificators (clList) does not have any items. It's an empty list. Place a break point in your code and check this.Resources["clList"] and check the count of items in it.

I see multiple problems.
in callMyClassificators, you create a new instance of MyClasificators. That instance is not the one you bind in Xaml. When you define a local resource, one instance is created there. That's the one your Combox is bound to, not the one you create in callMyClassificators. You should make sure xaml and code work on the same instance.
Let's say you fix No.1. When is "callMyClassificators" called? After the binding is done, there is no way for your MyClasificators to notify WPF that the list has changed. You could use a ObservableCollection> so that collection change will automatically be observed by WPF.

Related

Rebinding Observable collection wpf

I created a WPF window to bind data in Datagrid based on the selection of date by the user.BY default it loads for a specific date which works fine. But When the date is changed, the grid is showing empty rows.but the observable collection I used is having data. The observable collection is of DataTable type.
Note: I used to set itemsource=null when there are no records since I am using the same grid for 2 different tables based on a radio button check.
I have set the public variable as Binding variable in the XAML, used INOtifyChanged interface.
My issues is when the same collection reloads, the Datagrid failed to bind and shows empty rows but generating columns. When the previous attempt set the itemsource as null, the current loading failed to load the column also.
Any generic scenario, I am facing,? Please help
My Code:
private ObservableCollection<DataTable> custInfoCol = new ObservableCollection<DataTable>();
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private DataTable _CustInfo;
public DataTable CustInfo
{
get { return _CustInfo; }
set
{
_CustInfo = value;
PropertyChanged(this, new PropertyChangedEventArgs("CustInfo"));
}
}
private void rbPrev_Checked(object sender, RoutedEventArgs e)
{
custInfoCol.Clear();
custInfoCol.Add(CustInfo = showcustomer(cid));
}
Sounds like you might need to change the UpdateSourceTrigger in your binding:
Height="{Binding Height, ElementName=Day, UpdateSourceTrigger=PropertyChanged}"
I've had problems with the default behavior of it before, so now I just type it out every time.
https://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Based on this piece of code you provided in the comment:
<DataGrid ItemsSource="{Binding custInfo,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
I think there is typo in the Xaml. You should use ... Binding CustInfo... with capital C.
Of course, if AutoGenerateColumns is true, then I guess this is not the problem, because you wrote
Datagrid failed to bind and shows empty rows but generating columns
Which the Binding works. Therefore, you need to check showcustomer(cid) and make sure it contains the required DataRows.
I fixed the issue which is very simple. I had created the datatable instance as global and i changed it to local just before loading the executereader() method.It resolved the issue.

Where can I use a DataTemplate in something that isn't a repeater type of control?

Ok, I know it's kind of weird, but I am trying to create a data driven panel that reconfigures itself based on a DataTemplate member that I have on a object. Right now I am accomplishing this by using an ItemsControl bound against a dummy list of 1 bogus item so that I get a single instance of the data template. It just seems silly to have to do this in an ItemsControl, but I can't think of anything that will use my DataTemplate without trying to do it against a list of items. Anyone have any idea?
Just for clarity, let's say I have a Widget class:
public class Widget
{
public string Name { get; set; }
public DataTemplate MyTemplate { get; set; }
public List<object> DummyList = new List<object> { new object(); }
}
and the Xaml something like:
<ItemsControl ItemsSource={Binding DummyList} ItemTemplate={Binding MyTemplate}/>
I can then create a collection of Widgets and populate each one with the correct data template based on the object's status.
Anyway, as I said, this works... I'd just like to find a more elegant solution than using an ItemsControl if anyone knows of one.
Chances are that you could also just set ContentTemplate="{Binding template}" if your control (that you wish to dynamically modify its contents - e.g. Button inside etc.) is ContentControl. I found that often 'overlooked' as it's not immediately visible or intuitive, but saves you adding extra 'content'.
Or you can use ContentControl - or presenter as suggested already.
<ContentPresenter ContentTemplate="{Binding MyTemplate}"/>

Why does one of MY WPF DataGrids give the "'EditItem' is not allowed for this view" exception?

I have read all the Q&A I could find here and on the MS forums about this exception, and tried most of the suggestions that I understood, and a few others. It seems that this exception can come up for a wide range of causes.
As with others, I have a WPF DataGrid bound to a collection, which throws this exception when one tries to edit one of the cells. They are set to be write-able, the collection is an ObservableCollection, I've implemented get and set handlers which send notification messages.
The suggestions I haven't tried are the ones involving implementing IList's non-generic interface, because I have no idea what I would do to do that. Also, I have many DataGrids bound to various lists and collections in my app which work, and this one used to work when it was bound to a LINQ collection.
Please help me figure out what I need to do here.
The Data Grid is:
<DataGrid Name="dgIngredients" Margin="567,32,0,44" Width="360" ItemsSource="{Binding}" IsReadOnly="False"
AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Width="63" Header="Percent" Binding="{Binding Preference}" IsReadOnly="False" />
<DataGridTextColumn SortDirection="Descending" Width="301" Header="Ingredient" Binding="{Binding Ingredient}" IsReadOnly="True" CanUserSort="True" CanUserReorder="False" />
</DataGrid.Columns>
</DataGrid>
The column being edited is the non-read-only one, Preference.
The collection is:
private ObservableCollection<RAM_Ingredient> MemberIngredientPrefs = new ObservableCollection<RAM_Ingredient>();
The binding is:
dgIngredients.DataContext = MemberIngredientPrefs.OrderBy("Ingredient",true);
RAM_Ingredient is:
public class RAM_Ingredient : INotifyPropertyChanged
etc.
Where RAM_Ingredient.Preference is:
private int _Preference;
public int Preference
{
get
{
return _Preference;
}
set
{
// This is needed to send notification of changes (and to not throw an exception on grid edit!):
if ((_Preference != value))
{
SendPropertyChanging();
_Preference = value;
SendPropertyChanged("Preference");
}
}
}
The exception is:
System.InvalidOperationException was unhandled
Message='EditItem' is not allowed for this view.
Source=PresentationFramework
StackTrace:
at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item)
at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem)
at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)
etc...
I have also this problem, And found that the point here is that we can not edit a IEnumerable in a DataGrid, only a list can be edited.
therefore we didn't need to create a new class, its works also on a LINQ query with anonymous return type. it's need only to be a list.
here is a sample of my code:
dtgPrdcts.ItemsSource= ProductLists.Select(Function(x) New With {.ListTitle = x.ListTitle, .ProductID = x.ProductID, .License = "", .ForRemove = True}).ToList
I still don't know what specifically caused the problem, but I managed to work around it, and I'm not sure how much of what I did was overkill, but it works.
I created a new class just for the purpose of holding the data in the DataGrid rows. I make a List of objects of this class and fill it in and bind it to the DataGrid as I was doing before. I also added the usual stuff and nonsense for getting Change Notification to work (probably overkill) and I had to re-define a comparison function in a different way to get it to sort because of that whole comedy situation.
i.e.
List<UsablePref> MemberIngredientPrefs = new List<UsablePref>();
...
foreach (RAM_Ingredient ingredient in App.Ingredients)
{
ingredient.GetPreferences(EditorMember);
UsablePref pref = new UsablePref();
pref.Ingredient = ingredient.Ingredient;
pref.IngredientID = ingredient.IngredientID;
pref.Preference = ingredient.Preference;
MemberIngredientPrefs.Add(pref);
}
// Sort alphabetically by ingredient name,
MemberIngredientPrefs.Sort(UsablePref.CompareByName);
// and bind the ingredient prefs DataGrid to its corresponding List
dgIngredients.DataContext = MemberIngredientPrefs;
I had this same problem trying to create a list of rows from a join; since the LINQ query returns an IEnumerable, I had the DataGrid bound to that IEnumerable; this worked fine for readonly and oddly worked with ComboBoxes and some other custom controls I used, but plain text editing threw the InvalidOperationException. The solution was an ObservableCollection in place of the IEnumerable; basically from:
BoundView = (/*LINQ QUERY*/); // is IEnumerable<CustomJoinObject>
to
BoundView = new ObservableCollection<CustomJoinObject>(/*LINQ QUERY*/);
In both cases BoundView is the DataContext for the DataGrid.
I'm assuming this happens because IEnumerable doesn't have the machinery to support a datagrid, whereas ObservableCollection does.
The model class needs to implement the interface INotifyPropertyChanged coming from the namespace System.ComponentModel.
Class example:
public class Exemple : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion INotifyPropertyChanged Members
}
even if this thread is old, it could help someone.
The issue is that LINQ returns a IEnumerable, as said VaelynPhi, but the cause is that editing a datagrid requires a source that implements IEditableCollectionView.
You can then use a list, an observableCollection or any collection implementing this interface.
I found this solution thanks to Shoe who gave an answer on this tread.
In my case this exception was thrown when I wanted to edit some cells. The problem was of wrong collection type bound to ItemSource => when I switched from IEnumerable<T> to ObservableCollection<T> everything works correctly.

WPF bindings not refreshing

I have a listbox on my form that looks like this:
<ListBox Name="lbResults" SelectionChanged="lbResults_SelectionChanged"/>
I am binding the following collection to it:
ObservableCollection<Hand> oHands = new ObservableCollection<Hand>();
using the following code:
lbResults.DataContext = oHands;
Binding binding = new Binding();
lbResults.SetBinding(ListBox.ItemsSourceProperty, binding);
The oHands collection gets populated via a background worker that announces via an event whenever a new Hand object is available. The ListBox refreshes perfectly when something is added. The ToString() result of the Hand object is displayed and that is what I want - so far so good. However, when the background worker finishes
void finder_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
oHands = new ObservableCollection<Hand>(oHands.OrderBy(o => o.PotSize));
lbResults.SetBinding(ListBox.ItemsSourceProperty, new Binding());
}
The items in the list are still showing up in the original order. I can confirm that the list is re-ordered but the items are still showing up in the original order. How do I refresh this binding?
you dont want to assign oHands a new collection. just Clear() the collection then add the results from the operation. don't update the binding
instead of replacing the entire observable collection, you could just clear it and add all your new items. that wouldn't affect your binding.
You could also use a CollectionViewSource as your binding, and set the order on that instead of reordering the whole collection.
Wouldn't it be a lot easier to just set the itemsource directly?
lbResults.ItemsSource = oHands;
You're really just supposed to inherit from the INotifyPropertyChanged interface, but heres another way to force an update to a binding:
BindingExpression exp = BindingOperations.GetBindingExpression(lbResults, Listbox.ItemsSourceProperty)
exp.UpdateTarget()
Edit: I also just noticed you aren't setting any binding in the XAML and appear to be doing it programmatically with an empty Binding. I haven't tried that way before, so see if changing your XAML to this might help:
<ListBox Name="lbResults" SelectionChanged="lbResults_SelectionChanged" ItemsSource="{Binding Path=oHands}"/>
Then you set lbResults.DataContext to point to the class that has the member oHands. This is what worked for me in my project (in IronPython, so forgive me if my examples didn't convert to C# perfectly).

Is it possable to add a DataField to a Silverlight DataForm without creating an entire edit template?

I am using the DataForm for an entity with about 40 attributes. I'm happy with how the form displays all but 3 of the attributes. These 3 attributes happen to be lists of items.
I don't want to have to code out an entire edit template, seems very counter productive.
<dataFormToolkit:DataForm AutoGenerateFields="True" CurrentItem="{Binding XXX, Mode=TwoWay, Source={StaticResource XXXViewModel}}" >
<dataFormToolkit:DataField Label="Client" >
<ListBox ItemsSource="{Binding Client}"></ListBox>
</dataFormToolkit:DataField>
</dataFormToolkit:DataForm>
The the WCF RIA Services includes a Silverlight Business Application project template that demonstrates creating a CustomDataForm where they override OnAutoGeneratingField and modify the field for just the attributes you want. I've copied the code here for you to illustrate the idea but I'd suggest you check out the real thing to see how they are using the ReplaceTextBox extension method to deal with the Data Binding as well. Download link.
public class CustomDataForm : DataForm
{
protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
{
// Get metadata about the property being defined
PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);
// Do the password field replacement if that is the case
if (e.Field.Content is TextBox && this.IsPasswordProperty(propertyInfo))
{
e.Field.ReplaceTextBox(new PasswordBox(), PasswordBox.PasswordProperty);
}
// Keep this newly generated field accessible through the Fields property
this.fields[e.PropertyName] = e.Field;
// Call base implementation (which will call other event listeners)
base.OnAutoGeneratingField(e);
}
}
It will work : try that
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class IsPassword : System.Attribute { }
public class CustomDataForm : DataForm
{
protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
{
// Get metadata about the property being defined
PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);
// Do the password field replacement if that is the case
var attributes = propertyInfo.GetCustomAttributes(typeof(IsPassword), false).ToList();
if (attributes.Any(obj=>obj is IsPassword))
{
PasswordBox box= new PasswordBox();
Binding binding = new Binding(e.PropertyName);
binding.Mode = BindingMode.TwoWay;
box.SetBinding(PasswordBox.PasswordProperty, binding);
e.Field.Content=box;
}
base.OnAutoGeneratingField(e);
}
}
then just add [IsPassword] to your property
I'm pretty sure it's not possible. If I were you I would swallow my grief and create that edit template.
The only alternative I can see is to work with the data in your viewmodel and create a separate class that holds the 37 properties that need no changing. Then you make a separate entity for the 3 that need special attention. This way you could have two data forms, one autogenerated and one custom. Hopefully you can then work with styling them so they look like one form. A lot of work, I know, but it might be even more work to create the full edit template.

Resources