wpf relativesource binding in style datatemplate not working

essamce 621 Reputation points
2023-08-30T22:34:08.64+00:00

hi, my .Net Framework wpf app, xaml binding doesn't work, here is the code:

<UserControl
    x:Class="SegSolution.UI.Views.DocumentNavigationView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:SegSolution.Data;assembly=SegSolution.Data"
    xmlns:models="clr-namespace:SegSolution.UI.Models"
    xmlns:vm="clr-namespace:SegSolution.UI.ViewModels" 
    xmlns:local="clr-namespace:SegSolution.UI.Views">

    <UserControl.DataContext>
        <Binding />
        <!-- temp -->
        <!--<vm:DocumentNavigationViewModel/>-->
    </UserControl.DataContext>
    
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/SegSolution.UI;component/themes/generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Style TargetType="{x:Type TreeViewItem}" 
                   BasedOn="{StaticResource CurrentTheme.Control}">
                <!--<Setter Property="IsExpanded" Value="True" />-->
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="VerticalAlignment" Value="Top" />
            </Style>

            <Style x:Key="this.treeViewItemTemplate" TargetType="TextBlock">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="FontSize" Value="14" />
                <Setter Property="Text" Value="{Binding Path=Name}" />
                <Setter Property="ContextMenu" >
                    <Setter.Value>
                        <ContextMenu  >
                            <MenuItem Header="Select" 
                                Command="{Binding DataContext.SelectAcadObjCmd, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                CommandParameter="{Binding}" />
                            <MenuItem Header="Zoom To" IsEnabled="False" />
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
           
            </Style>
            <!-- DataType="{x:Type models:SegPartData}"-->
            <!--DataType="{x:Type models:SegUCSData}"-->
            <HierarchicalDataTemplate x:Key="this.treeviewTemplate"
                                     
                ItemsSource="{Binding Path=UCSs}"  >
                <TextBlock Style="{StaticResource this.treeViewItemTemplate}"
                    Margin="0 8 0 0" >
                    <TextBlock.RenderTransform>
                        <TranslateTransform Y="-4" />
                    </TextBlock.RenderTransform>
                </TextBlock>
                <HierarchicalDataTemplate.ItemTemplate >
                    <DataTemplate >
                        <TextBlock Style="{StaticResource this.treeViewItemTemplate}" />
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
            
        </ResourceDictionary>
    </UserControl.Resources>
    

    
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="30" />
            </Grid.ColumnDefinitions>
            
            <TextBox Grid.Column="0" 
                Text="{Binding DocName}" 
                ToolTip="{Binding FullDocName}"
                FontSize="14" IsReadOnly="True"
                     BorderThickness="0"
                     Foreground="White"
                     Background="Transparent"/>
            <Button Grid.Column="1" 
                Command="{Binding RefreshCmd}"
                ToolTip="Refresh/reload data..."
                Content="R" />
        </Grid>

        <TreeView Grid.Row="1" 
                 
            Background="Transparent" Margin="0 10 0 0"
            BorderThickness="0" 
            VerticalAlignment="Stretch"
            ItemsSource="{Binding PartsData}" 
            ItemTemplate="{StaticResource this.treeviewTemplate}"/>
        
    </Grid>
</UserControl>

the problem in line :

<MenuItem Header="Select" 
                                Command="{Binding DataContext.SelectAcadObjCmd, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                CommandParameter="{Binding}" />
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,527 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
778 questions
{count} votes