0

I use the textName 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??

4

2 に答える 2

0

同じ問題を抱えている次の人のために、私自身の答えを返信します。Mode=OneWay で動作させることができなかったので、これを行いました:

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 + ")" };

    }

そうすれば、OnlyRead のソースである Anonimous Type を使用する代わりに、

于 2011-09-24T11:17:48.663 に答える