DataTemplate to Linq
I have a ListBox of Dockpanels which display "FieldName:, [_____] (user input textbox)". After the user populates the field, I'm looking for a LINQ way to take the pairs and throw them into a KeyValuePair object.
<DataTemplate x:Key="ExtraLoginInfoTemplate">
<DockPanel>
<TextBlock Name="CodeID" Text="{Binding Path=ID,Converter={BLL:CodeMarkupExtension}}" />
<TextBox Name="Input"/>
</DockPanel>
</DataTemplate>
<ListBox Name="extraLoginInfoListBox" ItemsSource="{Binding}" ItemTemplate="{StaticResource ExtraLoginInfoTemplate}"/>
//codebehind
extraLoginInfoListBox.DataContext = cvList; //list of codevalue objects
private void su开发者_如何学CbmitButton_click(object sender, RoutedEventArgs e)
{
KeyValuePair<string,string> myInputs = /* ? some linq query to get the data from extraLoginInfoListBox */
}
You need a property to be bound with your Input textbox to store whatever value was entered by user:
<TextBox Name="Input" Text="{Binding Path=IDValue, Mode=TwoWay}" />
And then, you can use following code:
var keyValuePairs = cvList.ToDictionary((obj) => obj.ID, (obj) => obj.IDValue);
精彩评论