gpt4 book ai didi

c# - 将文本框绑定(bind)到 Func(Linq 查询)

转载 作者:太空狗 更新时间:2023-10-30 01:34:06 26 4
gpt4 key购买 nike

我正在做一个副业项目,在四处寻找之后我遇到了瓶颈,需要一些帮助。

情况是这样的:我有一个窗口,我想根据组合框中的选择动态填充它(简单),所以我以编程方式构建所有内容。我需要构建的是几个框,它们将根据同一结果集中的不同查询进行填充。我打算做的是将(文本框文本属性的)Binding.Source 设置为 Func,当调用更新源时,它将自动神奇地运行该函数。

那不会发生。关于如何将文本属性绑定(bind)到随时间变化的 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为绑定(bind)源(参数名为bindingSource)

    var binding = new Binding
{
Source = bindingSource,
Mode = BindingMode.OneWay
};
tb.SetBinding(TextBox.TextProperty, binding);

最佳答案

必须要有某种“魔力”。在您的情况下,它将是一个将 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";
}
}

关于c# - 将文本框绑定(bind)到 Func<T>(Linq 查询),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com