I am trying to bind a DataGrid.ItemSource
to a ObservableCollection<SearchObject>
. SearchObject is a custom class which has some public member variables (one is named "parameters"). Parameters is a custom class and has a public member variable (of type string) named "query". How can I get the "query" string to show up in the datagrid.
I tried Binding="{Binding ElementName=parameters, Path=query}"
within a DataGridTextColumn
, but it didn't seem to work. I imagined this would reference the parameters object and then look for its query member variable, but this didn't seem to work.
Any ideas? Here is my XAML:
<DataGrid HorizontalAlignment="Stretch" Name="watchListDataGrid"
VerticalAlignment="Stretch" IsReadOnly="True"
AlternatingRowBackground="#FFE4F0FC"
HorizontalScrollBarVisibility="Disabled"
SelectionChanged="watchListDataGrid_SelectionChanged"
CanUserReorderColumns="False"
CanUserSortColumns="False"
AutoGenerateColumns="False" KeyUp="watchListDataGrid_KeyUp">
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Header="Remove" Click="MenuRemoveWatchListItem_Click" />
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn Header="Query" Width="*"
Binding="{Binding ElementName=parameters, Path=query}" />
</DataGrid.Columns>
Assuming your DataGrid's ItemsSource is bound to something like:
<DataGrid ItemsSource={Binding Path=parameters}>
The DataGridTextColumn's Binding should be:
<DataGridTextColumn Binding="{Binding Path=query}" />
You use ElementName to reference another element within your XAML. For example, if you wanted to bind the text of a TextBlock to the text of a TextBox:
<TextBox Name="myTextBox" />
<TextBlock Text={Binding Path=Text, ElementName=myTextBox} />
I was able to resolve the problem. Yazan, your suggestion to use this code DID work:
<DataGridTextColumn Binding="{Binding Path=query}" />
However, I was missing { get; set; } for the SearchObject's "parameters" member variable. When I added these, it all worked fine:
public class SearchObject : ISerializable
{
public SearchParameters parameters { get; set; }
Thanks again for your help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.