views:

163

answers:

3

I'm having a problem with my dependency property. It seems like it doesn't want to work at all, and I don't know why. Here is the code for my dependency property:

public static readonly DependencyProperty CheckBoxColumnVisibilityProperty =
    DependencyProperty.Register("CheckBoxColumnVisibility", typeof(Visibility), typeof(ComputersControl), null);

public Visibility CheckBoxColumnVisibility
{
    get
    {
        return (Visibility)GetValue(CheckBoxColumnVisibilityProperty);
    }
    set
    {
        SetValue(CheckBoxColumnVisibilityProperty, value);
    }
}

Here is where I assign the value of my dependency property:

  <ComputerControl:ComputersControl Canvas.Left="50" BorderBrush="Black" Background="Blue" Name="computerControl" CheckBoxColumnVisibility="Collapsed" />

Here is where I am trying to bind it to a column in a DataGrid to make it so I can set that column to be visible or collapsed:

<Data:DataGrid.Columns>
  <Data:DataGridTemplateColumn 
    x:Name="CheckBoxColumn"
    Visibility="{Binding CheckBoxColumnVisibility}"
    Header="Selected">
      <Data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox x:Name="DataGridCheckBox"
            Click="DataGridCheckBox_Click"
            IsChecked="{Binding IsSelected, Mode=TwoWay}" 
            HorizontalAlignment="Center" />
        </DataTemplate>
      </Data:DataGridTemplateColumn.CellTemplate>
  </Data:DataGridTemplateColumn>

Can someone tell my why it isn't working? Every example I look at looks exactly the same as this, but mine doesn't work...

+1  A: 

try Visibility="{Binding CheckBoxColumnVisibility ElementName=computerControl}"

This only works on SL3+

Rob Fonseca-Ensor
I have set the itemsource for the datagrid. to a list of data. Is there anyway to still bind the CheckBoxVisibility property to that column in the xaml or am I stuck with doing it in code? I did get it to work but I had to set the visibility in the controls Loaded method.
Brett
A: 

You can use ElementName binding in Silverlight 3. Here is an example that might work:

<ComputerControl:ComputersControl Canvas.Left="50" BorderBrush="Black" Background="Blue" Name="computerControl" CheckBoxColumnVisibility="Collapsed" />

...


<Data:DataGridTemplateColumn 
x:Name="CheckBoxColumn"
Visibility="{Binding Path=CheckBoxColumnVisibility, ElementName=computerControl}"
Header="Selected">
  <Data:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <CheckBox x:Name="DataGridCheckBox"
        Click="DataGridCheckBox_Click"
        IsChecked="{Binding IsSelected, Mode=TwoWay}" 
        HorizontalAlignment="Center" />
    </DataTemplate>
  </Data:DataGridTemplateColumn.CellTemplate>

Jason Jackson
Actually that doesnt work because the Datagrid is inside the computerControl...
Brett
A: 

I have to use the DependencyPropertyMetaData when I create a DependencyProperty to call a PropertyChangedCallback in order to bind to the value of the dependency property inside my user control. Of course this is still in code but it works.

Brett