Sapper
Sapper

Reputation: 295

How to access my datagrid control in my viewmodel from view?

Hi guys I want to access my datagrid control in my viewmodel.I know this is the incorrect way but I have to do that:

<DataGrid
       Grid.Row="1" 
       Margin="10,10,0,0"
       Height="200"
       Width="500"
       Grid.ColumnSpan="2"
       Name="dg"
       HorizontalAlignment="Left"
       AutoGenerateColumns="False"
       ItemsSource="{Binding SSID}"
       >
                <DataGrid.Columns>
                    <DataGridTextColumn Width="100" Header="Network ID" Binding="{Binding _networkID}"></DataGridTextColumn>
                    <DataGridTextColumn Width="100" Header="SSID" Binding="{Binding _ssid}"></DataGridTextColumn>
                    <DataGridTextColumn Width="100" Header="VLAN" Binding="{Binding _vlan}"></DataGridTextColumn>


</DataGrid.Columns>


void AddSSIDs()
        {
            var ssid = new SSIDPropertyClass();

            ssid._networkID = SSID.Count + 1;

            ssid._ssid = EnteredSSIDAC;

            ssid._vlan = VlanSSID;

            if (ACSelectedSecurityType=="Static WEP")
            {
                ssid._authenticationMode = ACSelectedSecurityType;

                ssid._authentication = ACStaticWEPSelectedAuthentication;

                ssid._staticWEPKeyType = ACStaticWEPSelectedKeyType;

                ssid._staticWEPKeyLength = ACStaticWEPSelectedKeyLength;

                ssid._staticWEPKey1 = StaticWEPKey1;

                ssid._staticWEPKey2 = StaticWEPKey2;

                ssid._staticWEPKey3 = StaticWEPKey3;

                ssid._staticWEPKey4 = StaticWEPKey4;

                SSID.Add(ssid);


            }

            else if(ACSelectedSecurityType=="WPA/WPA2 Personal")
            {

                ssid._authenticationMode = ACSelectedSecurityType;

                ssid._wpaPersonalKeyAC = WpaACKey;

                SSID.Add(ssid);
            }

        }

I want to display only that columns in Datagrid which are selected in if blocks and else if blocks .If the condition of first if block is satisfies than all the other columns present inother else if blocks should be hidden. Please tell me the way in which I can access datagrid directly in ViewModel or any other way by which I can achieve the same thing.Any help would be highly appreciable.

Upvotes: 0

Views: 2254

Answers (4)

Den
Den

Reputation: 380

You Can bind colunm visibility prop to your viewmodel prop:

<DataGridTextColumn Width="100" Header="Network ID" Binding="{Binding _networkID}" Visibility="{Binding NetworkVisibility}"></DataGridTextColumn>
<DataGridTextColumn Width="100" Header="SSID" Binding="{Binding _ssid}" Visibilty="{Binding  SSIDVisible, Converter={StaticResource  SSIDVisible}}"></DataGridTextColumn>

In ViewModel

    public Visibility NetworkVisibility 
    {
     get { 
            if(condition) return Visibility.Visible;
        else return Visibility.Collapsed;
         }
    }

or you can do your viewmodel props of type bool, then just use BoolToVisibilityConverter in XAML

 public bool SSIDVisible
    {
        get { 
                if(condition) return true;
            else return false;
            }
    }

And for this props you can use NotifyPropertyChanged (if its supposed to change dynamically) as in Andrew Stephens answer.

Upvotes: 1

Andrew Stephens
Andrew Stephens

Reputation: 10203

You could expose a couple of boolean properties from your VM, indicating which set of columns to display, then bind the Visibility property of each column to the relevant property. You'll need to use the BooleanToVisibilityConverter to convert the boolean value to a Visibility value (Visible or Collapsed). Something like this:-

<Window.Resources>
    <BoolToVisibilityConverter x:Key="boolToVisConv" />
</Window.Resources>

<DataGridTextColumn Visibility="{Binding ShowWep, Converter={StaticResource boolToVisConv}" ... />    
<DataGridTextColumn Visibility="{Binding ShowWpa, Converter={StaticResource boolToVisConv}" ... />

Edit (some VM code as requested) Your VM class should implement INotifyPropertyChanged, and its property setters must raise the PropertyChanged event when the value changes. This ensures that anything in the view bound to a property reacts (e.g. refreshes) when its value changes. A typical example of the INPC interface can be found see here. Based on this code, the ShowWpa property would look something like this:-

public class MyViewModel
{
    private bool _showWpa; 

    public bool ShowWpa
    {
        get
        {
            return _showWpa;
        }
        set
        {
            if (_showWpa != value)
            {
                _showWpa = value;
                NotifyPropertyChanged("ShowWpa");
            }
        }
    }

    //etc..
}

Upvotes: 1

bit
bit

Reputation: 4487

A bad practise, but since you want it to be done that way..

Pass it as a parameter to the ViewModel from the code behind of the view.

Upvotes: 0

iop
iop

Reputation: 312

You could create properties which contain information about the column selection status, for example a bool value, and bind them to the Visible property of your column. Use a converter to convert from bool to Visibility.

Upvotes: 1

Related Questions