Reputation: 88
I have a window with different items and different views for each type. I would like to access the button in the window (parent) from my code behind from the content control.
OutputConfigView:
<ContentControl Margin="5">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedOutputRendererTyp}" Value="{x:Type outTypes:XmlBarcodeRenderer}">
<Setter Property="ContentTemplate" Value="{StaticResource OutputRendererView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding SelectedOutputRendererTyp}" Value="{x:Type outTypes:CsvBarcodeRenderer}">
<Setter Property="ContentTemplate" Value="{StaticResource CsvOutputRendererView}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
.
.
<Button x:Name="CloseButton"
Grid.Column="1"
Click="WindowClose"
Content="Ok"
Margin="0 0 0 -5"
HorizontalAlignment="Center"
VerticalAlignment="Bottom">
</Button>
OutputRendererView.xaml.cs:
public partial class OutputRendererView : UserControl
{
public OutputRendererView()
{
InitializeComponent();
}
private void Border_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var parentWindow = (OutputRendererView)this.Parent;
var button = (Button)parentWindow.FindName("CloseButton");
}
In this case the parentWindow is null.
How can I access the button of the calling window from the code behind of my control ?
Upvotes: 0
Views: 223
Reputation: 6638
To access the button inside the user control, you must first find the usercontrol
and then the button
inside it.
var button = (Button)((UserControl)parentWindow.FindName("userControlName")).FindName("CloseButton");
Upvotes: 0