views:

65

answers:

1

Hi All, What exactly I want to do is that there are 2 tables,ie, user and userprofile and both of them have almost identical fields. I shall take example of the email field. There is a textbox and the User table email field value is displayed in it. What I want to do is, have a context menu such that when the user right clicks on the textbox, the menu displays both the User and UserProfile email field values. – developer 1 hour ago
Whatever value one selects from the context menu the textbox then displays that value. You can use Binding Email1 and Binding Email2, as I have no problems getting those two values from database so I shall change my code accordingly. As I am new to WPF and .NET framework itself, I am not sure how to achieve this. Please let me know if I have made myself clear this time. I am not sure how to handle commands and events. Can anybody show me the code to accomalish this..

  <TextBox Style="{StaticResource FieldStyle}" Text="{Binding Email1, UpdateSourceTrigger=PropertyChanged}">
                            <TextBox.BorderBrush>
                                <MultiBinding Converter="{StaticResource TextBoxBorderConverter}">
                                    <Binding Path="Email1"/>
                                    <Binding Path="Email2"/>
                                </MultiBinding>
                            </TextBox.BorderBrush>
                        </TextBox>

Thanks in advance

+1  A: 

At risk of giving you a WPF/MVVM noob answer and getting flamed, here goes. I can't advise you on databinding with databases since I've never done it, so I will just give you the XAML and it's up to you to work on the database end.

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  <Grid>  
      <TextBox Height="28" Text={Binding PreferredEmail}">
        <TextBox.ContextMenu>
          <ContextMenu>
            <MenuItem Header="{Binding Email1}" Command="{Binding Email1Command}" />
            <MenuItem Header="{Binding Email2}" Command="{Binding Email2Command}" />
          </ContextMenu>
        </TextBox.ContextMenu>
      </TextBox>
  </Grid>
</Page>

In the databinding to objects case, PreferredEmail, Email1, and Email2 would bind to a dependency property or a property that raises the PropertyChanged event. This is how your ViewModel (or whatever you want to call the lower-level code) will update the data. If you change those values in code-behind, ultimately it'll get reflected in the context menu automagically. Then you have to implement two ICommand-based classes to handle the setting of PreferredEmail.

I think it's super lame to implement two command handlers, and it certainly won't scale well if you have to add more email sources. I think a better solution would be to use one command handler and a CommandParameter that is the selected MenuItem header, but I don't know how to do that. But in any case, the two command handler solution will still work if you're in a bind.

Dave
I am really sorry guys for not framing the question correctly. I am new to WPF so I might be mission out on technical details needed. What exactly I want to do is that there are 2 tables,ie, user and userprofile and both of them have almost identical fields. I shall take example of the email field. There is a textbox and the User table email field value is displayed in it. What I want to do is, have a context menu such that when the user right clicks on the textbox, the menu displays both the User and UserProfile email field values.
developer
..Continued.. Whatever value one selects from the context menu the textbox then displays that value. You can use Binding Email1 and Binding Email2, as I have no problems getting those two values from database so I shall change my code accordingly. As I am new to WPF and .NET framework itself, I am not sure how to achieve this. Please let me know if I have made myself clear this time.
developer
Dave from the above code that you have posted, the Header will give the email1 value for context menu but what will Command="{Binding Email1Command}" do. I have no Idea how to code that in a way that shall have the email1 or email2 value diplayed in textbox when user selects it..
developer
@developer: you should update your post with the comments you made to my answer, so it's clear to everyone that reads it for the first time. The Command="{Binding Email1Command}" binds the button to a command that gets executed when you click on it. The command needs to be derived from ICommand, and when you implement it, you can specify what to do when it executes, and whether or not the command can be executed. If the command cannot be executed (determined by your logic), then the button will be grayed out automatically. Read up on MVVM, it is a very nice pattern to use for WPF.
Dave
link to Josh Smith's very popular article: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Dave
Also read up on http://msdn.microsoft.com/en-us/magazine/cc785480.aspx for implementation on Commands.
Jeff Wain