Modifying or Accessing a Control in a Silverlight Form Change Template

I ran into an interesting problem today when you are playing with the Silverlight Data Form control. I wanted to change the visibility of a specific control inside an associated edit template. See below for xaml.

<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 depending on how access to the container of this data form was achieved to disable or hide the last two data fields. I did a job that had two data forms, but this is a bit overkill! Does anyone know how to access these controls inside an edit template?

+2


a source to share


1 answer


There may be another answer to this question, but I did to create a partial class for my data class, and in the partial I added a property that returns an enum of visibility, then set the visibility in code, control.

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

 }

      

In my actual XAML, I would bind to the visibility controls with something like this:



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

      

Hope this helps Cheers

+3


a source







All Articles