Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a user Control. Inside user Control I have a datagrid name CustomerGrid fill with customer names. now I have loaded that user control to main window now i want to simply bind selected row or selected customer to textbox in main window.
eg

TextBox Text = "{Binding ElementName=CustomerGrid ,Path=SelectedItem[0]}"

please help me i am Beginner in WPF.
Thanks
Posted

Hi,
you have to fix your binding path and bind to SelectedItem Property (without s).

Generally, you shouldn't bind directly to elements when dealing with data. Check the MVVM pattern.
 
Share this answer
 
Comments
Abu Khaleel 25-Feb-15 7:26am    
This is My Main Window Xaml Code:

<window x:class="NEPFXML.MainWindow"
="" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:customerdata="clr-namespace:NEPFXML" icon="Images\1410369140_network.png" title="Shakeel" height="800" width="1500" minwidth="500" minheight="500" x:name="CustomerForm" activated="CustomerForm_Activated" windowstartuplocation="CenterScreen" windowstate="Maximized">

<window.resources>
<customerdata:customerslist x:key="List">

<grid removed="#333">

<grid.rowdefinitions>

<rowdefinition height="50">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="*">

<grid.columndefinitions>
<columndefinition x:name="GridColumn" width="300" minwidth="300" maxwidth="300">
<columndefinition width="150">
<columndefinition width="150">
<columndefinition width="150">
<columndefinition width="150">
<columndefinition width="161">
<columndefinition width="273">

<customerdata:customerslist x:name="CList" grid.row="1" grid.rowspan="9">
<Button Content="Save" Grid.Row="8" Grid.Column="2" Style="{StaticResource StyleButton}" Click="Button_Click" />
<Button Content="Update" Grid.Row="8" Grid.Column="3" Style="{StaticResource StyleButton}" Click="Button_Click" />
<Button Content="Delete" Grid.Row="8" Grid.Column="4" Style="{StaticResource StyleButton}" Click="Button_Click" />
<Button Content="Details" Grid.Row="8" Grid.Column="5" Style="{StaticResource StyleButton}" Click="Button_Click" />

<textblock x:name="FullName" grid.columnspan="3" text="OK" grid.row="0" grid.column="2" fontsize="28" foreground="#E5E4E2" margin="5">



And This is My User Control Xaml Code:

<usercontrol x:class="NEPFXML.CustomersList"
="" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="300" d:designwidth="300" loaded="UserControl_Loaded">
<grid>
<grid.rowdefinitions>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>

<grid.columndefinitions>
<columndefinition>
<columndefinition>
<columndefinition>
<columndefinition>
Hello all
I have a user Control. Inside user Control I have a datagrid name CustomerGrid fill with customer names. now I have loaded that user control to main window now i want to simply bind selected row or selected customer to textbox in main window.
eg

TextBox Text = "{Binding ElementName=CustomerGrid ,Path=SelectedItem[0]}"

please help me i am Beginner in WPF.
Thanks




This is my Main window.Xaml Code:


<window x:class="NEPFXML.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomerData="clr-namespace:NEPFXML"
Icon="Images\1410369140_network.png"
Title="Shakeel" Height="800" Width="1500" MinWidth="500" MinHeight="500"

x:Name="CustomerForm" Activated="CustomerForm_Activated" WindowStartupLocation="CenterScreen" WindowState="Maximized">

<window.resources>
<customerdata:customerslist x:key="List" xmlns:customerdata="#unknown">

<grid background="#333">

<grid.rowdefinitions>

<rowdefinition height="50">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="40">
<rowdefinition height="*">

<grid.columndefinitions>
<columndefinition x:name="GridColumn" width="300" minwidth="300" maxwidth="300"> <columndefinition width="150"> <columndefinition width="150"> <columndefinition width="150"> <columndefinition width="150"> <columndefinition width="161"> <columndefinition width="273">
<customerdata:customerslist x:name="CList" grid.row="1" grid.rowspan="9" xmlns:customerdata="#unknown">





<textblock x:name="FullName" grid.columnspan="3" text="Binding ElementName=Clist,Path=CustomerGrid.SelectedItem[0]" grid.row="0" grid.column="2" fontsize="28" foreground="#E5E4E2" margin="5">



And This is my User Control.XAML Code:


<usercontrol x:class="NEPFXML.CustomersList" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
<grid>
<grid.rowdefinitions>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>
<rowdefinition>

<grid.columndefinitions>
<columndefinition> <columndefinition> <columndefinition> <columndefinition> <columndefinition> <columndefinition>
<expander grid.row="0" grid.column="0" grid.rowspan="2" grid.columnspan="6" header="Search" foreground="#E5E4E2" isexpanded="False" horizontalalignment="Left" verticalalignment="Top" margin="0,-10,0,0" expanddirection="Down">


<textbox style="{StaticResource StyleTextBox}" x:name="SearchBox" tooltip="Enter Customer Name to Search" width="290" horizontalalignment="Center" verticalalignment="Top" textchanged="SearchBox_TextChanged" margin="5,5,0,5" isreadonly="False">

<datagrid style="{StaticResource StyleDataGrid}" rowstyle="{StaticResource StyleDataGridRow}" cellstyle="{StaticResource StyleDataGridCell}" horizontalalignment="Left" x:name="CustomerGrid" grid.row="1" grid.rowspan="8" grid.columnspan="6">



 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900