绑定ContextMenu DataContext适用于TextBlock,但不适用于TextBox

时间:2016-09-27 13:12:45

标签: c# wpf xaml binding contextmenu

我在我的视图中得到了这个,我需要能够通过右键单击 - >编辑TreeView中的Skeleton项目的名称。上下文菜单 - >改名。有一个小问题:

当我使用TextBlock时它工作正常(TextBlock不可编辑)但是当我使用TextBox时绑定关闭。我尝试了几种绑定和祖先类型,但无济于事。不确定我是否应该切换到TextBox。

    <UserControl.Resources>
    <Image x:Key="visibilityImage" Source="../Resources/visibility.png" Height="16" Width="16" />
    <Image x:Key="visibilityOffImage" Source="../Resources/visibilityoff.png" Height="16" Width="16" />

    <Style TargetType="{x:Type ToggleButton}" x:Key="visibilityButtonStyle">
        <Setter Property="Content" Value="{DynamicResource visibilityImage}" />
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Content" Value="{DynamicResource visibilityOffImage}" />
            </Trigger>
        </Style.Triggers>
    </Style>
    <DataTemplate DataType="{x:Type commonVM:GyroSuitViewModel}">
        <views:GyroSuitView></views:GyroSuitView>
    </DataTemplate>
    <DataTemplate DataType="{x:Type commonVM:SkeletonViewModel}">
        <views:SkeletonView></views:SkeletonView>
    </DataTemplate>
</UserControl.Resources>
<Grid Name="Root">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <StackPanel Orientation="Vertical">
            <Label Content="{Binding Title}" Margin="2"/>
            <TreeView Name="SkeletonItems" ItemsSource="{Binding Skeletons}" commonExtensions:RightClickTreeViewSelectionExtension.IsEnabled="True">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="True"/>
                        <Setter Property="Foreground" Value="{StaticResource ForegroundBrush}"/>
                        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                        <Style.Resources>
                            <Style TargetType="{x:Type Border}">
                                <Setter Property="Grid.ColumnSpan" Value="2" />
                            </Style>
                        </Style.Resources>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=Records}">
                        <DockPanel LastChildFill="True">
                            <ToggleButton Background="Transparent" BorderThickness="0" Width="20" Height="20" IsChecked="{Binding IsVisible, Mode=TwoWay}" >
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding ChangeVisibilityCommand}"/>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction Command="{Binding ChangeVisibilityCommand}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                                <Image Stretch="Fill"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                                    <Image.Style>
                                        <Style TargetType="{x:Type Image}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsVisible}" Value="true">
                                                    <Setter Property="Source" Value="../Resources/visibility.png"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding IsVisible}" Value="false">
                                                    <Setter Property="Source" Value="../Resources/visibilityoff.png"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </ToggleButton>
                            <ToggleButton Visibility="{Binding RecordingButtonVisibility}" Background="Transparent" BorderThickness="0" Width="20" Height="20" IsChecked="{Binding IsRecording, Mode=TwoWay}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding RecordCommand}"/>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction Command="{Binding RecordCommand}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                                <Image Stretch="Fill"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                                    <Image.Style>
                                        <Style TargetType="{x:Type Image}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsRecording}" Value="true">
                                                    <Setter Property="Source" Value="../Resources/stop.png"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding IsRecording}" Value="false">
                                                    <Setter Property="Source" Value="../Resources/record.png"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </ToggleButton>
                            <Button Visibility="{Binding DeleteButtonVisibility}"  Name="DeleteButton" Height="20" BorderThickness="0" Command="{Binding DeleteCommand}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
                                <Image Source="../Resources/delete.png" Stretch="Fill"/>
                            </Button>
                            <xctk:ColorPicker SelectedColor="{Binding SkeletonColor, Converter={converter:DrawingColorToMediaColorConverter}}" BorderThickness="0" Margin="2" StandardColors="{Binding ElementName=Root, Path=DataContext.AvailableSkeletonColors}" Background="Transparent" Width="20" ShowAdvancedButton="False" ShowAvailableColors="False" ShowDropDownButton="False" ShowStandardColors="True" StandardColorsHeader="" />
                            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}">
                                <TextBlock.ContextMenu>
                                    <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Visibility="{Binding Path=ContextMenuVisibility}">
                                        <MenuItem Header="Rename"/>
                                        <MenuItem Header="Open Containing Folder" Command="{Binding OpenFolderCommand}"/>
                                    </ContextMenu>
                                </TextBlock.ContextMenu>
                            </TextBlock>
                        </DockPanel>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
            <ContentPresenter Name="details" Content="{Binding SelectedItem, ElementName=SkeletonItems}" />
        </StackPanel>
    </ScrollViewer>
</Grid>

当我使用TextBox时,这些是输出中的错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Root'. BindingExpression:Path=DataContext.AvailableSkeletonColors; DataItem=null; target element is 'ColorPicker' (Name=''); target property is 'StandardColors' (type 'ObservableCollection`1')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'TextBox' (Name=''); target property is 'Tag' (type 'Object')

感谢您的每一个输入!

0 个答案:

没有答案