将文本框绑定到Func <t>(Linq查询)

时间:2015-08-11 05:43:29

标签: c# wpf linq wpf-4.0

我正在进行一个侧面项目,经过多次探索之后我已经撞墙了,可以使用一些帮助。

情况如下:我有一个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);

2 个答案:

答案 0 :(得分:3)

有些东西必须做“神奇”。在你的情况下,它将是一个将lambda表达式转换为字符串的转换器。

private class MyWebViewClient extends WebViewClient {
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
          if (Uri.parse(url).getHost().equals("www.example.com")) { 
             return false; 
          }
          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));                          
          startActivity(intent); return true;
    }
} 

答案 1 :(得分:0)

您是否实施了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));
    }
}
相关问题