将文本框绑定到 Func<T>(Linq 查询)
Bind Textbox to Func<T> (Linq query)
我正在做一个业余项目,我在四处寻找后遇到了困难,需要一些帮助。
情况如下:我有一个 Window,我想根据组合框中的选择动态填充(简单),因此我以编程方式构建所有内容。我需要构建的是几个框,它们将根据同一结果集中的不同查询进行填充。我打算做的是将 Binding.Source(文本框文本 属性)设置为 Func,当调用更新源时它会自动神奇地 运行 该函数。
那不会发生。关于如何将文本 属性 绑定到随时间变化的 LINQ 查询,有什么想法吗?
我可以提供更多需要的信息。
谢谢,
尼克
更新片段:
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;
}
设置func为绑定源(参数名为bindingSource)
var binding = new Binding
{
Source = bindingSource,
Mode = BindingMode.OneWay
};
tb.SetBinding(TextBox.TextProperty, binding);
"magic" 必须做点什么。在您的情况下,它将是一个将 lambda 表达式转换为字符串的转换器。
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";
}
}
你实施INotifyPropertyChanged
了吗?
对于您的 viewmodel/data 上下文,这种简单的方法怎么样:
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));
}
}
我正在做一个业余项目,我在四处寻找后遇到了困难,需要一些帮助。
情况如下:我有一个 Window,我想根据组合框中的选择动态填充(简单),因此我以编程方式构建所有内容。我需要构建的是几个框,它们将根据同一结果集中的不同查询进行填充。我打算做的是将 Binding.Source(文本框文本 属性)设置为 Func,当调用更新源时它会自动神奇地 运行 该函数。
那不会发生。关于如何将文本 属性 绑定到随时间变化的 LINQ 查询,有什么想法吗?
我可以提供更多需要的信息。
谢谢, 尼克
更新片段:
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;
}
设置func为绑定源(参数名为bindingSource)
var binding = new Binding
{
Source = bindingSource,
Mode = BindingMode.OneWay
};
tb.SetBinding(TextBox.TextProperty, binding);
"magic" 必须做点什么。在您的情况下,它将是一个将 lambda 表达式转换为字符串的转换器。
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";
}
}
你实施INotifyPropertyChanged
了吗?
对于您的 viewmodel/data 上下文,这种简单的方法怎么样:
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));
}
}