Friday, April 23, 2010

WPF Binding Control, It works!

Well I am a little dense but I finally figured it out.  Used this article:


Now when I select a server from my drop down list it populates the correct text box using binding.  :) !


<StackPanel DataContext="{Binding}" Width="444" Height="341">
            <StackPanel.Resources>
              
                <!-- Instantiate the object ServerControls and call the method GetServerList,  This goes out and builds a list of Server Objects
                that have the server name, ip, and port-->
                <ObjectDataProvider x:Key="servers"
                                    ObjectType="{x:Type svc:ServersControl}"
                                    MethodName="GetServerList"></ObjectDataProvider>

                <!-- Format how each server object will be displayed.  Name in bold and blue, IP and port below seperated with a colon-->
                <DataTemplate   x:Key="serverLayout" DataType="Server">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="Blue"></TextBlock>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Ip}"></TextBlock>
                            <TextBlock Text=":"> </TextBlock>
                            <TextBlock Text="{Binding Path=Port}"></TextBlock>
                        </StackPanel>

                    </StackPanel>
                </DataTemplate>

            </StackPanel.Resources>
          
            <!-- Create my ListBox and bind it to the resources I created above.-->
            <ListBox Height="200" Name="listBox1" Width="Auto" ItemsSource="{Binding Source={StaticResource servers}}"
                                                              ItemTemplate="{DynamicResource serverLayout}"
                                                              IsSynchronizedWithCurrentItem="True"
                                                              SelectedItem="{Binding SelectedItem}"
                                                            
                                                               >
            </ListBox>
            <Label Content="Select a server from the list or manually input below." Height="27" Name="label1" Width="286" />


            <!--I had to include a data context for the grid I was putting my text boxes in-->
            <Grid Width="Auto" Height="Auto" Background="AliceBlue" DataContext="{Binding ElementName=listBox1, Path=SelectedItem}" >


                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                
                </Grid.RowDefinitions>



                <TextBlock Margin="5" Text="IP:" Grid.Column="0" FlowDirection="LeftToRight"></TextBlock>
                <TextBox Margin="5" Grid.Column="1" Name="TextBoxIP" Text="{Binding Path=Ip}" />


                <TextBlock Margin="5" Text="Port:" Grid.Column="2"></TextBlock>
                <TextBox Margin="5" Grid.Column="3" Name="TextBoxPort" Text="{Binding Path=Port}"></TextBox>

                <TextBlock Margin="5" Text="UserName:" Grid.Column="0" FlowDirection="LeftToRight" Grid.Row="2"></TextBlock>
                <TextBox Text="Administrator" Name="TextBoxUsername" Margin="5" Grid.Column="1" Grid.Row="2"></TextBox>
                <TextBlock Margin="5" Text="Password:" Grid.Column="2" Grid.Row="2"></TextBlock>
                <PasswordBox Name="TextBoxPwd" Margin="5" Grid.Column="3" Grid.Row="2" ></PasswordBox>
              
              

            </Grid>



No comments:

Post a Comment