locked
Data binding RRS feed

  • Question

  • Is it possible to bind the IsEnabled Button property to the load/unload of a FlowDocument?

    VM

    Thursday, January 23, 2014 3:15 PM

Answers

  • Any idea why I can´t get this DataBinding working?

    VM


    The Button is disabled because you are binding to a Command and the CanExecute method of this command return false.

    If you bind an ICommand to the Buttons Command property, you should handle logic of enabling and disabling int the CanExecute method of this command:

    <Window.CommandBindings>
            <CommandBinding Command="ApplicationCommands.Open" CanExecute="CommandBinding_CanExecute"/>
        </Window.CommandBindings>
        <StackPanel>
            <Button x:Name="LoadContentButton" Height="22" Cursor="Hand" Margin="2,0,0,0" Command="ApplicationCommands.Open" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Command.Name}" CommandTarget="{Binding ElementName=MainWindow}" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text=" Load Content" />
     </StackPanel>
    </Button>
    
    <FlowDocumentScrollViewer x:Name="FDSV" Grid.Row="1" Grid.Column="1" Margin="2" IsToolBarVisible="False" >
     <FlowDocument x:Name="FDPlainText">
      <Paragraph>
       <Run x:Name="PlainTextRun" />
      </Paragraph>
     </FlowDocument>
    </FlowDocumentScrollViewer>
    
            private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = FDPlainText != null && FDPlainText.IsLoaded;
            }
    
    Please refer to the following link for more information: http://msdn.microsoft.com/en-us/library/ms753200(v=vs.110).aspx

    • Marked as answer by Vítor Manuel Friday, January 31, 2014 10:49 AM
    Wednesday, January 29, 2014 8:41 AM

All replies

  • There is an IsLoaded property that corresponds the Loaded and UnLoaded events of a FrameworkContentElement which you can bind to. You need to explicitly update the binding target when the events fire though:

            <Button x:Name="btn" Content="Button" IsEnabled="{Binding Path=IsLoaded, ElementName=flowDoc}"/>
    
            <RichTextBox>
                <FlowDocument x:Name="flowDoc" Loaded="flowDoc_Loaded" Unloaded="flowDoc_Unloaded">
                    <Paragraph>
                        Text...
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
    

     
     private void flowDoc_Loaded(object sender, RoutedEventArgs e)
            {
                BindingExpression bo = BindingOperations.GetBindingExpression(btn, Button.IsEnabledProperty);
                bo.UpdateTarget();
            }
    
            private void flowDoc_Unloaded(object sender, RoutedEventArgs e)
            {
                BindingExpression bo = BindingOperations.GetBindingExpression(btn, Button.IsEnabledProperty);
                bo.UpdateTarget();
            }
    

    Thursday, January 23, 2014 4:38 PM
  • private void FDPlainText_Loaded(object sender, RoutedEventArgs e) { BindingExpression bo = BindingOperations.GetBindingExpression(LoadContentButton, Button.IsEnabledProperty); bo.UpdateTarget(); } private void FDPlainText_Unloaded(object sender, RoutedEventArgs e) { BindingExpression bo = BindingOperations.GetBindingExpression(LoadContentButton, Button.IsEnabledProperty); bo.UpdateTarget(); } <Button x:Name="LoadContentButton" IsEnabled="{Binding ElementName=FDPlainText, Path=IsLoaded}" Height="22" Cursor="Hand" Margin="2,0,0,0" Command="ApplicationCommands.Open" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Command.Name}" CommandTarget="{Binding ElementName=MainWindow}" > <StackPanel Orientation="Horizontal"> <Image Width="16" Height="16" Source="/Images/Load16.png" />

    <TextBlock Text=" Load Content" /> </StackPanel> </Button> <FlowDocumentScrollViewer x:Name="FDSV" Grid.Row="1" Grid.Column="1" Margin="2" IsToolBarVisible="False" > <FlowDocument x:Name="FDPlainText" Loaded="FDPlainText_Loaded" Unloaded="FDPlainText_Unloaded" MouseLeftButtonDown="WordClick_MouseLeftButtonDown" > <Paragraph> <Run x:Name="PlainTextRun" /> </Paragraph> </FlowDocument> </FlowDocumentScrollViewer>


    Thank you Magnus for the answer.

    I did a try, but the button doesn't change when FlowDocument is loaded/unloaded.

    Thursday, January 23, 2014 6:42 PM
  • Any idea why I can´t get this DataBinding working?

    VM

    Tuesday, January 28, 2014 9:59 PM
  • Any idea why I can´t get this DataBinding working?

    VM


    The Button is disabled because you are binding to a Command and the CanExecute method of this command return false.

    If you bind an ICommand to the Buttons Command property, you should handle logic of enabling and disabling int the CanExecute method of this command:

    <Window.CommandBindings>
            <CommandBinding Command="ApplicationCommands.Open" CanExecute="CommandBinding_CanExecute"/>
        </Window.CommandBindings>
        <StackPanel>
            <Button x:Name="LoadContentButton" Height="22" Cursor="Hand" Margin="2,0,0,0" Command="ApplicationCommands.Open" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Command.Name}" CommandTarget="{Binding ElementName=MainWindow}" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text=" Load Content" />
     </StackPanel>
    </Button>
    
    <FlowDocumentScrollViewer x:Name="FDSV" Grid.Row="1" Grid.Column="1" Margin="2" IsToolBarVisible="False" >
     <FlowDocument x:Name="FDPlainText">
      <Paragraph>
       <Run x:Name="PlainTextRun" />
      </Paragraph>
     </FlowDocument>
    </FlowDocumentScrollViewer>
    
            private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = FDPlainText != null && FDPlainText.IsLoaded;
            }
    
    Please refer to the following link for more information: http://msdn.microsoft.com/en-us/library/ms753200(v=vs.110).aspx

    • Marked as answer by Vítor Manuel Friday, January 31, 2014 10:49 AM
    Wednesday, January 29, 2014 8:41 AM
  • "There is an IsLoaded property that corresponds the Loaded and UnLoaded events of a FrameworkContentElement which you can bind to..."

    It seems that the IsLoaded property you suggested corresponds to the load/unload events of the FlowDocument element itself not its content, therefore that's not an option.

    Friday, January 31, 2014 10:47 AM