我有多个视频显示它们与Mainviewmodel中的视频选择绑定.一切正常,直到我尝试将enter命令绑定到Mainviewmodel.我不知道这个的语法.目前,绑定设置为Video而不是Mainviewmodel.
错误信息:
'StartVideoCommand' property not found on 'object' ''Video'
Run Code Online (Sandbox Code Playgroud)
XAML:
<Window.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MainViewModel}">
<ListBox ItemsSource="{Binding Videos}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.InputBindings>
!!! <KeyBinding Key="Enter" Command="{Binding StartVideo}" /> !Bound to Video not to Mainviewmodel grrr
</Grid.InputBindings>
... layout stuff
<TextBlock Text="{Binding Title}" Grid.Column="0" Grid.Row="0" Foreground="White"/>
<TextBlock Text="{Binding Date}" Grid.Column="0" Grid.Row="1" Foreground="White" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Length}" Grid.Column="1" Grid.Row="1" Foreground="White" HorizontalAlignment="Right"/>
... closing tags
Run Code Online (Sandbox Code Playgroud) 在我的Windows Phone 8应用程序中,我有项目列表.我为列表项定义了ItemTemplate.
我想在每个项目中显示一个来自视图模型的值,而不是列表项本身.我应该如何设置从列表项到viewmodel的绑定.
我的数据模板是这样的:
<DataTemplate x:Key="template">
<StackPanel>
<TextBlock Text="{Binding Name}"/> <!-- From list item -->
<TextBlock Text="{Binding MyViewModel.Country ?? }"/> <!-- From view model -->
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
那么如何将Country属性绑定到视图模型,而不是列表项(在项目源中).
这是基于XAML的应用程序(WPF/Silverlight/WinRT)中的常见情况.
WPF相关链接 - > WPF数据绑定:如何访问"父"数据上下文?
RelativeSource用AncestorType,并且Mode=FindAncestor通常在WPF中进行救援.
WinRT API中缺少这两个.如何访问父母(可能不是直接的)DataContext,?
(我知道TemplateBinding,ElementBinding但两者都不适用于DataTemplate).
需要的简短说明:我需要使用ViewModel的DataContext中的方法触发DataTemplate中的按钮命令.
问题的简短说明:模板化按钮命令似乎只能绑定到项目本身的datacontext.WPF和Windows 8.1应用程序用于浏览可视化树的语法似乎不起作用,包括ElementName和Ancestor绑定.我非常希望不让我的按钮命令位于MODEL内部.
附注:这是使用MVVM设计方法构建的.
以下代码生成VIEW上的项目列表.该列表是每个列表项的一个按钮.
<ItemsControl x:Name="listView" Tag="listOfStories" Grid.Row="0" Grid.Column="1"
ItemsSource="{x:Bind ViewModel.ListOfStories}"
ItemTemplate="{StaticResource storyTemplate}"
Background="Transparent"
IsRightTapEnabled="False"
IsHoldingEnabled="False"
IsDoubleTapEnabled="False"
/>
Run Code Online (Sandbox Code Playgroud)
在同一个VIEW的页面资源中,我创建了一个DataTemplate,其中包含有问题的按钮.我继续并删除了按钮内的大部分格式,例如文本,以使代码更易于阅读.除了列出的问题(命令的绑定)之外,关于按钮的所有内容都有效.
<Page.Resources>
<DataTemplate x:Name="storyTemplate" x:DataType="m:Story">
<Button
Margin="0,6,0,0"
Width="{Binding ColumnDefinitions[1].ActualWidth, ElementName=storyGrid, Mode=OneWay}"
HorizontalContentAlignment="Stretch"
CommandParameter="{Binding DataContext, ElementName=Page}"
Command="{Binding Source={StaticResource Locator}}">
<StackPanel HorizontalAlignment="Stretch" >
<TextBlock Text="{x:Bind StoryTitle, Mode=OneWay}"
FontSize="30"
TextTrimming="WordEllipsis"
TextAlignment="Left"/>
</StackPanel>
</Button>
</DataTemplate>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
因为这是一个DataTemplate,所以DataContext已设置为组成列表(MODEL)的各个项目.我需要做的是选择列表本身的DataContext(VIEWMODEL),这样我就可以访问导航命令了.
如果您对VIEW页面的代码隐藏感兴趣,请参阅下文.
public sealed partial class ChooseStoryToPlay_View : Page
{
public ChooseStoryToPlay_View()
{
this.InitializeComponent();
this.DataContextChanged += (s, e) => { ViewModel = DataContext as ChooseStoryToPlay_ViewModel; };
}
public ChooseStoryToPlay_ViewModel …Run Code Online (Sandbox Code Playgroud) 我正在尝试将MainWindow.xaml中的int变量传递给UserControl.当我重新调试myGridSize总是等于零.我将不胜感激任何帮助.
x:Name="myWindow">
<Grid>
<my:SudokuGrid x:Name="mySudokuGrid" myGridSize="{Binding SudokuSize, ElementName=myWindow}">
</my:SudokuGrid>
</Grid>
Run Code Online (Sandbox Code Playgroud)
public static readonly DependencyProperty SudokuSizeProperty =
DependencyProperty.Register("SudokuSize", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(null));
private int SudokuSize
{
get { return (int)GetValue(SudokuSizeProperty); }
set { SetValue(SudokuSizeProperty, value); }
}
public MainWindow()
{
SudokuSize = 9;
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
x:Name="gridCustom">
<UniformGrid Name="SudokuUniGrid" Style="{StaticResource CustomGridStyle}">
</UniformGrid>
Run Code Online (Sandbox Code Playgroud)
public static readonly DependencyProperty myGridSizeProperty =
DependencyProperty.Register("myGridSize", typeof(int), typeof(SudokuGrid), new FrameworkPropertyMetadata(null));
public int myGridSize
{
get { return (int)GetValue(myGridSizeProperty); }
set { SetValue(myGridSizeProperty, value); }
} …Run Code Online (Sandbox Code Playgroud) 我需要在wpf xaml中访问parent的DataContext.整个xaml页面代码是动态的.所以不知道父母的类型.
我写这个
<Grid DataContext={Binding Path=.}>
Run Code Online (Sandbox Code Playgroud)
它是否正确?
wpf ×5
binding ×4
xaml ×4
c# ×2
datacontext ×2
data-binding ×1
datatemplate ×1
itemtemplate ×1
mvvm ×1
uwp ×1
viewmodel ×1
windows-8 ×1
winrt-xaml ×1