0

In my application I'm using a ribbon control, on this ribbon is a button. When I click the button it should get the selected value from the datagrid in my view.

Now when I click the button the value it gets is always null.

This is my code:

View1.xaml

<ScrollViewer Background="White" Grid.Row="1" Grid.Column="1" Margin="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible">
    <DataGrid SelectionMode="Single" ItemsSource="{Binding Collection}" SelectedItem="{Binding Test, Mode=TwoWay}" AutoGenerateColumns="False" Name="stackpanelLocked"  HorizontalAlignment="Left">
        <DataGrid.Columns>
            <DataGridTextColumn Header="RandomHeaderX" Binding="{Binding Path=X}"/>
            <DataGridTextColumn Header="RandomHeaderY" Binding="{Binding Path=Y}"/>
        </DataGrid.Columns>
    </DataGrid>
</ScrollViewer>

Viewmodel

//get selected item value
string lastSelectedValue;
private Model _test;        
public Model Test 
{
    get
    {
        return _test;
    }
    set
    {
        _test = value;
        if(Test != null)
            this.lastSelectedValue = string.Format("Variabele X: {0} Variabele Y: {1}", Test.X, Test.Y);
    }
}

private ICommand _ClickCommand;
public ICommand ClickCommand
{
    get
    {
        if (_ClickCommand == null)
            _ClickCommand = new DelegateCommand(new Action(Execute),
                        new Func<bool>(CanExecute));
        return _ClickCommand;
    }
}

public bool CanExecute()
{
    return true;
}

public void Execute()
{
    if(lastSelectedValue != null)
        Clipboard.SetText(lastSelectedValue);  
}

Model

public int X { get; set; }
public int Y { get; set; }

public Model(int x, int y)
{
    X = x;
    Y = y;
}

Extra info:

Every time I click in the datagrid it will actually fill the string lastSelectedValue. But when I click the button on the ribbon it will say that the lastSelectedValue is null.

Collection stuff:

public List<DatabaseClass.coord> AllCoords { get; set; }
public ObservableCollection<Model> Collection { get; set; }
public ModuleARibbonTabModel()
{
    Collection = new ObservableCollection<Model>();
    GenerateDatas();
}

private void GenerateDatas()
{
    using (var ctx = new DatabaseClass.TestDatabaseEntities1 ())
    {
        AllCoords = ctx.coord.ToList();
    }
    foreach(DatabaseClass.coord f in AllCoords)
    {
        Collection.Add(new Model(Convert.ToInt32(f.Een.ToString()), Convert.ToInt32(f.Twee.ToString())));
    }
}
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
MCollard
  • 926
  • 2
  • 20
  • 39
  • Please show your Collection property. – Anatolii Gabuza Feb 24 '14 at 10:11
  • Does this have something to do with threads maybe? – MCollard Feb 24 '14 at 10:26
  • 3
    With the code you've given there is no way for `lastSelectedValue` to be set to null after it has been set to a valid value. Since you say that it is being set by the datagrid ok, there must be a bug in code that you have not posted. Why don't you put a breakpoint on `lastSelectedValue` to see when it is being set back to null? – GazTheDestroyer Feb 24 '14 at 10:52
  • If you have an answer, please post it as an answer, not in your question. – Andrew Barber Apr 17 '14 at 19:07

2 Answers2

1

Bind to SelectedItem instead of SelectedValue.

Thomas Hetzer
  • 1,537
  • 13
  • 24
  • Thanks for taking your time to read my question. Why do people even upvote this.(not to be rude) I already tried that before, and I even set it to value for testing if it worked(because someone else on this site said it should be value....). – MCollard Feb 24 '14 at 09:41
  • Hi! I had the impression that you want to get the complete object your list is bound to. Than you should use SelectedItem. If you want only a value of your selected object, you should use SelectedValue and SelectedValuePath. – Thomas Hetzer Feb 24 '14 at 09:49
  • 1
    You have added some extra information. Are you sure, that the ribbon button and the view are bound to the datacontext instance? – Thomas Hetzer Feb 24 '14 at 10:04
0

I managed to work around the problem. I don't think this is a good fix but this is what I changed:
I changed the string lastSelectedValue to a static string lastSelectedValue This makes sure I can get the value.

MCollard
  • 926
  • 2
  • 20
  • 39