Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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# 设置x:gridview数据模板的数据类型_C#_Xaml_Uwp_Xbind - Fatal编程技术网

C# 设置x:gridview数据模板的数据类型

C# 设置x:gridview数据模板的数据类型,c#,xaml,uwp,xbind,C#,Xaml,Uwp,Xbind,我有一个GridView <GridView ItemsSource="{x:Bind ViewModel.SoundEffects}"> <GridView.ItemTemplate> <DataTemplate x:dataType="?"> <Button Style="{StaticResource defaultButton}" Content="{Bind

我有一个
GridView

<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:dataType="?">
            <Button Style="{StaticResource defaultButton}"
                    Content="{Binding Name}"
                    Tag="{Binding FileName}"
                    Command="{x:Bind ViewModel.PlayCommand}"
                    CommandParameter="{Binding FileName}"/>
        </DataTemplate>
    </GridView.ItemTemplate>
但这不起作用,导致整个调试器崩溃

谢谢你抽出时间

在DataTemplate内部(无论用作项模板、内容模板还是标题模板),Path的值不会在页面的上下文中解释,而是在被模板化的数据对象的上下文中解释。为了在编译时验证其绑定(并为其生成有效代码),DataTemplate需要使用x:DataType声明其数据对象的类型

因此,首先,您需要使您的模型如下所示:

public class SoundEffectButton 
{ 
    public string Name { get; set;} 
    public string FileName { get; set;} 
    public ICommand Play { get; set; }
}
xmlns:data="using:[the namespace of your model]
<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:SoundEffectButton">
            <Button Style="{StaticResource defaultButton}"
            Content="{Binding Name}"
            Tag="{Binding FileName}"
            Command="{x:Bind Play}"
            CommandParameter="{Binding FileName}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>
它是一个
,而不是一个接口。然后在页面中使用它,如下所示:

public class SoundEffectButton 
{ 
    public string Name { get; set;} 
    public string FileName { get; set;} 
    public ICommand Play { get; set; }
}
xmlns:data="using:[the namespace of your model]
<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:SoundEffectButton">
            <Button Style="{StaticResource defaultButton}"
            Content="{Binding Name}"
            Tag="{Binding FileName}"
            Command="{x:Bind Play}"
            CommandParameter="{Binding FileName}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>
GridView
x:DataType
可以设置如下:

<DataTemplate x:DataType="data:SoundEffectButton">
GridView
应该是这样的:

public class SoundEffectButton 
{ 
    public string Name { get; set;} 
    public string FileName { get; set;} 
    public ICommand Play { get; set; }
}
xmlns:data="using:[the namespace of your model]
<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:SoundEffectButton">
            <Button Style="{StaticResource defaultButton}"
            Content="{Binding Name}"
            Tag="{Binding FileName}"
            Command="{x:Bind Play}"
            CommandParameter="{Binding FileName}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

我为你的问题写了一封信,你可以看看

在DataTemplate内部(无论用作项模板、内容模板还是标题模板),Path的值不会在页面的上下文中解释,而是在被模板化的数据对象的上下文中解释。为了在编译时验证其绑定(并为其生成有效代码),DataTemplate需要使用x:DataType声明其数据对象的类型

因此,首先,您需要使您的模型如下所示:

public class SoundEffectButton 
{ 
    public string Name { get; set;} 
    public string FileName { get; set;} 
    public ICommand Play { get; set; }
}
xmlns:data="using:[the namespace of your model]
<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:SoundEffectButton">
            <Button Style="{StaticResource defaultButton}"
            Content="{Binding Name}"
            Tag="{Binding FileName}"
            Command="{x:Bind Play}"
            CommandParameter="{Binding FileName}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>
它是一个
,而不是一个接口。然后在页面中使用它,如下所示:

public class SoundEffectButton 
{ 
    public string Name { get; set;} 
    public string FileName { get; set;} 
    public ICommand Play { get; set; }
}
xmlns:data="using:[the namespace of your model]
<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:SoundEffectButton">
            <Button Style="{StaticResource defaultButton}"
            Content="{Binding Name}"
            Tag="{Binding FileName}"
            Command="{x:Bind Play}"
            CommandParameter="{Binding FileName}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>
GridView
x:DataType
可以设置如下:

<DataTemplate x:DataType="data:SoundEffectButton">
GridView
应该是这样的:

public class SoundEffectButton 
{ 
    public string Name { get; set;} 
    public string FileName { get; set;} 
    public ICommand Play { get; set; }
}
xmlns:data="using:[the namespace of your model]
<GridView ItemsSource="{x:Bind ViewModel.SoundEffects}">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:SoundEffectButton">
            <Button Style="{StaticResource defaultButton}"
            Content="{Binding Name}"
            Tag="{Binding FileName}"
            Command="{x:Bind Play}"
            CommandParameter="{Binding FileName}" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>


我在这里为您的问题写了一篇文章,您可以看看。

好的,现在我面临另一个问题,如何正确调用我的wavePlayer,它位于MainPageViewModel中,PlayCommand无法获得对MainPageViewModel的引用,因为CommandParameter是播放的文件名字符串,注意:我使用的是OK,所以现在我面临另一个问题,如何正确调用位于MainPageViewModel中的wavePlayer,PlayCommand无法获取对MainPageViewModel的引用,因为CommandParameter是播放的文件名字符串,注意:我正在使用