WPF数据上下文问题

当使用单一内容控件时,命令参数只设置绑定而不设置具体的源和路径时,默认找到上一层的数据上下文,没有就一直往上面查找知道找到数据上下文,当在集合内的控件使用数据模板并且设置了itemsources时,集合控件里面的子项如果不设定绑定的命令参数,则会默认找到itemsources对应的子项

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"  DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
    <Window.Resources>
        <x:Array x:Key="arr" Type="local:Car">
            <local:Car Height="10" Wide="200" Length="401"/>
            <local:Car Height="20" Wide="300" Length="402"/>
            <local:Car Height="30" Wide="400" Length="403"/>
            <local:Car Height="40" Wide="500" Length="404"/>
            <local:Car Height="50" Wide="600" Length="405"/>
        </x:Array>
        <local:MainViewModel x:Key="st"/>

        <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="Foreground" Value="White"/>
            <Style.Triggers>
                <Trigger Property="ListBox.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="CornflowerBlue"/>
                    <Setter Property="Foreground" Value="Black"/>
                </Trigger>
                <Trigger Property="ListBox.AlternationIndex" Value="2">
                    <Setter Property="Background" Value="LightBlue"/>
                    <Setter Property="Foreground" Value="Navy"/>
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
   <StackPanel>
            <TextBlock Height="20" Text="{Binding T.MyProperty}"></TextBlock>
            <Button Command="{Binding RelayCommand}" CommandParameter="{Binding}" Height="30"></Button>
            <ComboBox x:Name="com" ItemsSource="{Binding Source={StaticResource arr}}"  DisplayMemberPath="Height" SelectedIndex="0"></ComboBox>
            <ListBox ItemsSource="{Binding ElementName=com, Path=ItemsSource}" MouseDoubleClick="ListBox_MouseDoubleClick" Width="500">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Button Height="20" Width="50" 
                                Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext.RelayCommand}" 
                                CommandParameter="{Binding}"></Button>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <ListBox>
                <Button Height="20"  Width="200" Command="{Binding RelayCommand}" 
                        CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, Path=SelectedItem}">
                    
                </Button>
            </ListBox>
        </StackPanel>
    </Grid>

后台代码
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;

namespace WpfApp1.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
       
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
           
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real"
            }
            ///
            RelayCommand = new RelayCommand<object>(Add);
        }

        private void Add(object obj)
        {
         
        }

        private Teacher t=new Teacher();

        public Teacher T
        {
            get { return t; }
            set { t = value; RaisePropertyChanged(); }
        }
        public RelayCommand<Object> RelayCommand { get; set; }  
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值