views:

471

answers:

1

I came across an interesting issue today when playing around with the Silverlight Data Form control. I wanted to change the visibility of a particular control inside the bound edit template.. see xaml below.

<df:DataForm x:Name="NoteFormEdit" ItemsSource="{Binding Mode=OneWay}" AutoGenerateFields="True"
                AutoEdit="True" AutoCommit="False"
                CommitButtonContent="Save"
                CancelButtonContent="Cancel"               
                CommandButtonsVisibility="Commit"
                LabelPosition="Top" ScrollViewer.VerticalScrollBarVisibility="Disabled"
                EditEnded="NoteForm_EditEnded">
        <df:DataForm.EditTemplate>
            <DataTemplate>
                <StackPanel>
                    <df:DataField>
                        <TextBox Text="{Binding Title, Mode=TwoWay}"/>
                    </df:DataField>

                    <df:DataField>
                        <TextBox Text="{Binding Description, Mode=TwoWay}" AcceptsReturn="True" HorizontalScrollBarVisibility="Auto"
                                 VerticalScrollBarVisibility="Auto" Height="" TextWrapping="Wrap" SizeChanged="TextBox_SizeChanged"/>
                    </df:DataField>

                    <df:DataField>
                        <TextBlock Text="{Binding Username}" x:Name="tbUsername"/>
                    </df:DataField>

                    <df:DataField>
                        <TextBlock Text="{Binding DateCreated, Converter={StaticResource DateConverter}}" x:Name="tbDateCreated"/>
                    </df:DataField>
                </StackPanel>
            </DataTemplate>
        </df:DataForm.EditTemplate>
    </df:DataForm>

I wanted to depending on how the container of this data form was accessed to disable or hide the last two data fields. I did a work around which had two data forms but this is a bit excessive! Does anyone know how to access these controls inside the edit template?

A: 

There maybe another answer to this, but what I did was to create a partial class for my data class and in the partial I added a property that returns the Visibility enum, then set the visibilty in the code behind before binding to the control.

 public partial class MyDataClass
 {
    public Visibility IsVisible { get; set; }

 }

In my actual XAML I would bind to the controls Visibility Property something like this:

   <df:DataField>    
   <TextBlock  Visibility="{Binding  IsVisible }" Text="{Binding DateCreated, Converter={StaticResource DateConverter}}" x:Name="tbDateCreated"/>    

Hope that helps Cheers

Anthony