Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# BindingExpression路径错误-如何在MVVM模型中构造代码?_C#_Wpf_Mvvm_Binding - Fatal编程技术网

C# BindingExpression路径错误-如何在MVVM模型中构造代码?

C# BindingExpression路径错误-如何在MVVM模型中构造代码?,c#,wpf,mvvm,binding,C#,Wpf,Mvvm,Binding,我有一个按钮,点击时应该调整两个网格的宽度 视图模型 我不知道如何构造我的代码,以便网格的宽度属性可以保留在ViewModel中,但我的按钮的触发器也可以工作。我的视图的DataContext是ViewModel 有没有一个好的方法来实现这一点 感谢您的帮助。问题在于数据模板内的绑定:WPF尝试在MediaDetail类上查找属性路径ShowRightGridCommand,因为ItemsControl将其生成的每个项目容器的数据上下文设置为相应的项目 实际上,您要做的是绑定到视图模型。您可以这

我有一个按钮,点击时应该调整两个网格的宽度

视图模型

我不知道如何构造我的代码,以便网格的宽度属性可以保留在ViewModel中,但我的按钮的触发器也可以工作。我的视图的DataContext是ViewModel

有没有一个好的方法来实现这一点


感谢您的帮助。

问题在于数据模板内的绑定:WPF尝试在MediaDetail类上查找属性路径ShowRightGridCommand,因为ItemsControl将其生成的每个项目容器的数据上下文设置为相应的项目

实际上,您要做的是绑定到视图模型。您可以这样做:

{Binding ElementName=LeftGrid, Path=DataContext.ShowRightGridCommand}
LeftGrid是ItemsControl之外的元素。它的DataContext是您的MoviePanelViewModel实例,并且定义了ShowRightGridCommand属性

更简洁的方法是将名称root放在视图的根控件(通常是UserControl或Window)上,然后使用ElementName=root,Path=DataContext。。。。作为你的约束——至少我通常是这样做的


另一种方法是使用RelativeSource={RelativeSourceUserControl},但我发现ElementName=root更简单。

谢谢Frank-我不知道这一点。我决定使用ElementName=root,Path=DataContext。。。。方法
<Grid Grid.Row="2" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding LeftGridWidth}" />
            <ColumnDefinition Width="{Binding RightGridWidth}" />
        </Grid.ColumnDefinitions>

        // BUTTONS DISPLAYED HERE
        <Grid x:Name="LeftGrid" Grid.Row="2" Grid.Column="0" >
            <Border BorderThickness="1" BorderBrush="Red">
                <ItemsControl ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding _movies}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="5" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </Border>
        </Grid>

        <Grid x:Name="RightGrid" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="1" >
            <DockPanel>
                <StackPanel  VerticalAlignment="Top" Height="200">
                    <TextBlock Width="200" Height="50" DockPanel.Dock="Top" HorizontalAlignment="Left">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} ({1})">
                                <Binding Path = "SelectedMovie.title"/>
                                <Binding Path = "SelectedMovie.year"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
                <StackPanel VerticalAlignment="Top" Height="200">
                    <Grid HorizontalAlignment="Center">
                        <Image Source="star_icon.png" Width="100" Height="100" VerticalAlignment="Top"/>
                        <TextBlock Text="{Binding SelectedMovie.rating}" Style="{StaticResource AnnotationStyle}" Width="150"/>
                    </Grid>
                </StackPanel>
            </DockPanel>
        </Grid>
</Grid>
public List<MediaDetail> _movies { get; set; }
    public string selectedMovieID { get; set; }

    private GridLength _leftGridWidth;
    private GridLength _rightGridWidth;
    private readonly GridLength _defaultRightGridWidth = new GridLength(0, GridUnitType.Pixel);


    public MoviePanelViewModel()
    {
        ShowRightGridCommand = new DelegateCommand(SwitchRightGridWidth);
        LeftGridWidth = new GridLength(7, GridUnitType.Star);
        RightGridWidth = _defaultRightGridWidth;
    }

    private void SwitchRightGridWidth()
    {
        RightGridWidth = RightGridWidth == _defaultRightGridWidth ? new GridLength(3, GridUnitType.Star) : _defaultRightGridWidth;
    }

    public GridLength LeftGridWidth
    {
        get { return _leftGridWidth; }
        set { _leftGridWidth = value; OnPropertyChanged("LeftGridWidth"); }
    }


    public GridLength RightGridWidth
    {
        get { return _rightGridWidth; }
        set { _rightGridWidth = value; OnPropertyChanged("RightGridWidth"); }
    }

    public ICommand ShowRightGridCommand { get; set; }
BindingExpression path error: 'ShowRightGridCommand' property not found on 'object' ''MediaDetail'
{Binding ElementName=LeftGrid, Path=DataContext.ShowRightGridCommand}