Reputation: 353
I am working on a side project where I have hit a wall after much poking around and could use some help.
Here is the situation: I have a Window that I want to dynamically populate based on a choice in a combobox (easy) so I am building everything programmatically. What I need to build is several boxes that will populate based off of different queries in the same result set. What I was planning on doing was setting the Binding.Source (of the textbox text property) to a Func and that when update source was called then it would auto-magically run that function.
That doesn't happen. Any thoughts on how to bind a text property to a LINQ query that will change over time?
I can provide any more info that is required.
Thanks, Nick
Update Snippets:
private int AllelePopulation(IAllele allele)
{
var list= from b in _population.Populus
from g in b.Genes
where g.Representation == allele.Representation
select b;
return list.ToList().Count;
}
Setting the func as the binding source (parameter name is bindingSource)
var binding = new Binding
{
Source = bindingSource,
Mode = BindingMode.OneWay
};
tb.SetBinding(TextBox.TextProperty, binding);
Upvotes: 4
Views: 795
Reputation: 5205
Do you implement INotifyPropertyChanged
?
How about this simple approach for your viewmodel/data context:
public class DC : INotifyPropertyChanged
{
// access to you Func<string> is simply via a property
// this can be used by setting the binding in code or in XAML
public string Allele
{
get { return MyFunc(); }
}
// whenever a variable used in your function changes, raise the property changed event
private int I
{
get { return i; }
set { i = value; OnPropertyChanged("Allele"); }
}
private int i;
public void Modify()
{
// by using the property, the change notification is done implicitely
I = I + 1;
}
// this is your private int AllelePopulation(IAllele allele) function
private string MyFunc()
{
return (I*2).ToString();
}
// property changed notification
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Upvotes: 0
Reputation: 1574
Something has to do the "magic". In your case it would be a converter which converts a lambda expression to a string.
class Conv : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((Func<string>)value)();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var binding = new Binding
{
Source = (Func<string>)AllelePopulation,
Mode = BindingMode.OneWay,
Converter = new Conv()
};
textBox.SetBinding(TextBox.TextProperty, binding);
}
private string AllelePopulation()
{
return "test";
}
}
Upvotes: 3