开发者

Binding textbox to listbox twoway onewaytosource problems?

I use the textNa开发者_如何学编程me for the user to enter his name. Then typing, the textchanged event updates the listbox with the names that matchs with the input, then the user can click on an item (CompletedName in listbox), and when it happens I need the textbox updates with the item content.. This problem started to happen when I changed the "GivenName" (as a field from the table I query) for the "CompletedName".. (it is a string concat from the query as u see above)

I have this LINQ query:

var players =
                    from p in context.Player
                    where (p.GivenName.StartsWith(TextName.Text.Trim()) || p.Number.StartsWith(TextName.Text) || p.Surname.StartsWith(TextName.Text) )
                    select new { CompleteName = p.GivenName + " " + p.Surname + " (" + p.Number + ")"};

Then I make this the source for a listbox named listNames and I have this textbox:

<TextBox Name="TextName" Text="{Binding ElementName=listNames, Path=SelectedItem.CompleteName}"/>

When I run it, the next error is shown: "A Two Way or OneWayToSource binding cannot work on the read-only property 'CompleteName' of type '<>f__AnonymousType0`1[System.String]'"

I understand, of course that it can not be a TwoWay or OneWayToSource. But I need the user can add content to the textName, because it is also a search textbox, without updating the SelectedItem on the listbox.

If I add to the textbox the expression Mode=OneWay.. nothing happens in the textName control, I mean it doesnt show the item from the listbox.. What should I do for make it work??


You're binding to an instance of an anonymous type, but properties of anonymous types are read-only, so the binding can't update the CompleteName property.

Anyway, from what I understand, you're not trying to update the name, the TextBox is actually a search box. In that case the approach you're using cannot work. You need to handle the TextChanged event of the TextBox (or bind it to a property of your ViewModel if you're using MVVM), and use the new value to select the appropriate item in the ListBox. So anyway you won't be able to do this with an anonymous type, since its properties won't be accessible in the method that performs the search...


I will reply my own answer for next people with the same problem. As I couldn't make it work with the Mode=OneWay i did this:

public class CompleteNamesResuls
    {
        public String CompleteName { get; set; }
    }
  private void TextName_TextChanged(object sender, TextChangedEventArgs e)
    {
                var players =
                    from p in context.Player
                    where (p.GivenName.StartsWith(TextName.Text.Trim()) || p.Number.StartsWith(TextName.Text) || p.Surname.StartsWith(TextName.Text))
                    select new CompleteNamesResuls(){ CompleteName = p.GivenName + " " + p.Surname + " (" + p.Number + ")" };

    }

That way, instead of using the Anonimous Type that was the OnlyRead source of

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜