1

I have a UserControl within a frame on it's parent window. In the usercontrol, I have a textbox that needs to be edited when a button on the parent window is toggled.

I'm trying to do it using triggers UserControl.xaml

<UserControl.Resources>
    <ResourceDictionary>
        <Style x:Key="TextBoxEdit" TargetType="TextBox">
            <Setter Property="IsReadOnly" Value="True" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=EditButton}" Value="True">
                    <Setter Property="IsReadOnly" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>

</UserControl.Resources>
<Grid>
    <TextBox
        x:Name="EditTextBox"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Style="{StaticResource TextBoxEdit}"
        Text="Edit me" />
</Grid>

This works fine when there is a button named EditButton within the usercontrol, but is it possible to do this when the EditButton is within the parent window?

MainWindow.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ToggleButton x:Name="EditButton" HorizontalAlignment="Center" VerticalAlignment="Top">Edit</ToggleButton>
    <Frame Grid.Row="1" Source="Home.xaml" />
</Grid>

It is my understanding that the data context for the usercontrol will be inherited from the parent window, so is it possible to do it as simple as a trigger bound to the button or will I have to use a viewmodel/button commands?

1 Answers1

0

...but is it possible to do this when the EditButton is within the parent window?

No, it's not because then the Button and TextBox belong to different naming scopes.

It is my understanding that the data context for the usercontrol will be inherited from the parent window, so is it possible to do it as simple as a trigger bound to the button or will I have to use a viewmodel/button commands?

You should bind the IsChecked property of the ToogleButton to a source property of a view model, and then bind the trigger to same source property:

<DataTrigger Binding="{Binding IsChecked}" Value="True">
...

Make sure that the view model implements the INotifyPropertyChanged interface correctly: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks for that. I've updated my question with my viewmodel, where do I go from here? –  Aug 10 '17 at 13:44
  • Please ask a new question if you have another issue. – mm8 Aug 10 '17 at 13:44
  • Please see the new question here: https://stackoverflow.com/questions/45615689/wpf-mainwindow-toggle-property-in-usercontrol-via-viewmodel Thanks. –  Aug 10 '17 at 13:52