我有一个特定的xaml数据绑定问题.我有两个列表框(主 - 详细信息,因此列表框将IsSynchronizedWithCurrentItem设置为true).我希望我的viewmodel知道详细信息列表框中的所选项何时更改:我在viewmodel类上创建了一个int属性(即我们可以调用此属性SelInd)并在详细信息viewmodel上以这种方式绑定:
SelectedIndex="{Binding Mode=OneWayToSource, Path=SelInd}"
Run Code Online (Sandbox Code Playgroud)
我在运行时没有得到任何错误/异常,但绑定不会触发:当所选项目发生更改时,我的viewmodel属性不会更新.如果我将绑定模式更改为TwoWay,一切正常,但这不是我需要的.我需要它与OneWayToSource一起使用(顺便说一句,如果我将SelectedItem绑定到SelectedValue属性,则适用相同的非工作行为).
为什么这些绑定不会触发OneWayToSource?
这是一个更完整的代码示例,只是为了让事情更清楚:编辑:我无法显示真实代码(NDA)但我会在这里展示更简单和类似的东西(Page的DataContext是一个PageViewModel类的实例解释我只需要我的viewmodel类的SelInd属性应该始终在第二个ListBox中反映SelectedIndex的值.我已经找到了这样做的替代方法(代码隐藏中的事件处理程序或附加行为)但是现在我只是好奇它为什么它不适用于OneWayToSource绑定.
<Page>
<ContentControl x:Name="MainDataContext">
<Grid DataContext={Binding Path=Masters}>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
SelectionMode="Single"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding }">
<ListBox.ItemContainerStyle>
...
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
....
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Grid.Column="1"
SelectionMode="Single"
SelectedIndex="{Binding Mode=OneWayToSource, ElementName=MainDataContext,Path=DataContext.SelInd}"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Details}">
<ListBox.ItemContainerStyle>
...
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
....
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</ContentControl>
</Page>
Run Code Online (Sandbox Code Playgroud)
这是视图模型类的草图
public class PageViewModel{
public ObservableCollection<MasterClass> Masters {get;set;}
public int SelInd {get;set;}
....
}
Run Code Online (Sandbox Code Playgroud)
这里是MasterClass,它只是一个名字和一个细节列表
public class MasterClass{
public ObservableCollection<DetailsClass> Details …
Run Code Online (Sandbox Code Playgroud) 我有一个ItemsControl
,我已经构建了我在其中显示的项目(views:DisplayGroupView
),它们将水平扩展以显示所有内容而不是垂直(仅使用可用高度)
我已经改变了我ItemsPanel
的ItemsControl
使用StackPanel
与Orientation="Horizontal"
布局明智它是完美的,但无论我做什么我都不能让它水平滚动所以我可以看到一切.
这是以下的XAML ItemsControl
:
<ItemsControl ItemsSource="{Binding DisplayGroups}" Grid.Row="1" Margin="120,20,120,20" VerticalContentAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel Orientation="Horizontal" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<views:DisplayGroupView Margin="0,0,20,0" DataContext="{Binding}" VerticalAlignment="Stretch"></views:DisplayGroupView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
这样可以解决所有问题,但不会滚动.我也尝试更改ItemsControls模板以包含一个scrollviewer,但这只是垂直堆叠的东西:
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.VerticalScrollMode="Disabled">
<ItemsPresenter VerticalAlignment="Stretch"/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
Run Code Online (Sandbox Code Playgroud)
如何在能够滚动的同时获得水平布局?
我目前正在研究WPF应用程序,但我找不到如何全屏显示我的应用程序.我正在使用MahApps.Metro,所以我的主窗口类型是Controls.MetroWindow.
我试过这个:
<Controls:MetroWindow x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Views="clr-namespace:MyProject.Views"
WindowStyle="None"
ResizeMode="NoResize"
WindowState="Maximized"
Title="MyProject">
Run Code Online (Sandbox Code Playgroud)
但它不会隐藏Windows任务栏.但是当我使用一个简单的Window时,它可以工作.我看了一下MetroWindow的源代码,它继承了Window类,所以我不明白为什么它不起作用.
我发现使用Metro的全屏窗口的唯一方法是将IgnoreTaskbarOnMaximize属性设置为true并删除ResizeMode ="NoResize"(请参阅下面的代码),运行应用程序并最大化它.
<Controls:MetroWindow x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Views="clr-namespace:MyProject.Views"
WindowStyle="None"
IgnoreTaskbarOnMaximize="True"
Title="MyProject">
Run Code Online (Sandbox Code Playgroud)
但是我想隐藏最小化和最大化按钮..你有什么想法全屏启动Controls.MetroWindow吗?
我想了解一下.当我将View连接到ViewModel时,如下所示:
<DataTemplate DataType="{x:Type local:MyViewModel}">
<local:MyView />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
看起来View被设置为ViewModel的DataTemplate.但ViewModel没有DataTemplate的属性.那究竟是怎么回事呢?
问题的演示 - 我如何通过代码执行此操作(以特定方式连接View和ViewModel.我无法编写ViewModel.DataTemplate = View)?
谢谢.
在我的WPF窗口(.NET 4.0)中,我有两列Grid:左侧是拉伸文本框(或其他),右侧是Expander.同样在Expander中我有GridSplitter,它旨在扩展Expander时调整左右列的大小.但它不起作用.
这是我的XAML代码:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid ShowGridLines="True" >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" Name="column"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" HorizontalAlignment="Stretch" TextWrapping="Wrap"
Text="TextBox" VerticalAlignment="Stretch" Background="Aqua"/>
<Expander Grid.Column="1" Header="Expander" ExpandDirection="Left"
HorizontalAlignment="Right" Background="LightBlue" >
<Expander.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Some text Some text Some Text" Grid.Column="1"/>
<GridSplitter Grid.Column="0" Width="5"
ResizeBehavior="PreviousAndCurrent"
ResizeDirection="Columns"
HorizontalAlignment="Stretch"/>
</Grid>
</Expander.Content>
</Expander>
</Grid></Window>
Run Code Online (Sandbox Code Playgroud)
找到了合适的解决方案.XAML:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
</Window.Resources>
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" Name="leftColumn"/>
<ColumnDefinition Width="Auto"/> …
Run Code Online (Sandbox Code Playgroud) 我有一个包含两个项目的解决方案:名为"MyTest.TestApplication"的WPF应用程序和名为"MyTest.ClassLibrary"的类库,以及相应的默认名称空间.MyTest.TestApplication引用了MyTest.ClassLibrary.MyTest.ClassLibrary包含一个名为MyTest.ClassLibrary.TestClass的类.我想将此类的实例添加到应用程序的资源中,但使用URL作为Xml命名空间值.
所以,我打开MyTest.ClassLibrary.AssemblyInfo.cs,添加一个
using System.Windows.Markup
Run Code Online (Sandbox Code Playgroud)
指令,然后在最后我添加行
[assembly: XmlnsDefinition("http://schemas.test.com/test", "MyTest.ClassLibrary")]
Run Code Online (Sandbox Code Playgroud)
我构建项目,转到MyTest.TestApplication.App.Xaml,然后向Application元素添加名称空间定义.一旦我输入xmlns:t =",我就会得到一个intellisense的程序集名称和uris列表,但不是http://schemas.test.com/test.如果我还是添加命名空间定义,就像这样:
xmlns:t="http://schemas.test.com/test"
Run Code Online (Sandbox Code Playgroud)
并将以下内容添加到Resources元素:
<t:TestClass x:Key="myTest" />
Run Code Online (Sandbox Code Playgroud)
然后该资源没有被创建,并且Xaml编辑器抱怨名称空间http://schemas.test.com/test中不存在类TestClass .
令人沮丧的是,我添加了一个名为WpfCustomControlLibrary1的默认WPF自定义控件库,添加了该行
[assembly: XmlnsDefinition("bluh", "WpfCustomControlLibrary1")]
Run Code Online (Sandbox Code Playgroud)
到WpfCustomControlLibrary1.AssemblyInfo.cs,构建项目,在MyTest.TestApplication中引用它,并且一旦我开始输入xmlns:b ="在App.xam中,intellisense会在顶部显示名称为"bluh"的命名空间列表.
我尝试添加各种程序集和使用指令到MyTest.ClassLibrary,但我不能让Visual Studio通过架构URL找到我的程序集.令人惊讶的是,如果我将命名空间设置为程序集名称,它确实有效,如下所示:
xmlns:t="clr-namespace:MyTest.ClassLibrary;assembly=MyTest.ClassLibrary"
Run Code Online (Sandbox Code Playgroud)
那么......这里发生了什么?
编辑:令人沮丧的是,我通过简单地从WPF应用程序中删除引用,然后重新添加它,经过两个小时的搜索后终于开始工作了.啊.我猜这是某种VS2012错误?
我试图在我的Windows 8.1商店应用程序(XAML)中遵循MVVM模式.
我想在UI中单击/点击GridViewItem时导航到新视图.我想在没有代码的情况下执行此操作以提升可测试性(使用MVVM Light).
为了允许我的UI绑定到视图模型命令,我一直在查看通过添加引用 - > Windows - >扩展添加的Microsoft行为SDK(XAML).
我点击网格视图项时,我视图中的以下代码会编译但会爆炸.不幸的是它提供了很少的帮助,只是抛出了未处理的win32异常[3476].
有人可以帮助解决这个问题吗?
使用的命名空间是;
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
<GridView x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
ItemsSource="{Binding Source={StaticResource GroupedSource}}"
IsSwipeEnabled="True"
IsTapEnabled="True">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Margin="0"
Height="230">
<StackPanel Orientation="Vertical"
HorizontalAlignment="Stretch">
<Image Source="{Binding Image}"
Stretch="UniformToFill"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
<StackPanel VerticalAlignment="Bottom"
Height="45"
Margin="0,-45,0,0">
<StackPanel.Background>
<SolidColorBrush Color="Black"
Opacity="0.75"
/>
</StackPanel.Background>
<TextBlock FontSize="16"
Margin="2"
Text="{Binding Name}"
TextWrapping="Wrap"
VerticalAlignment="Bottom"
/>
</StackPanel>
</StackPanel>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding DataContext.SummaryCatagorySelectedCommand, ElementName=LayoutRoot}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
编辑.根据要求,我添加了视图模型,特别包含了我想从我的行为中触发的命令.
public class ViewModel : ViewModelBase
{ …
Run Code Online (Sandbox Code Playgroud) 我正在使用wpf弹出控件.
<Popup x:Name="tabHolder" IsOpen="False"
PopupAnimation="Slide" Placement="Bottom"
PlacementTarget="{Binding ElementName=mainWidgetWindow}">
<Grid Height="105" Width="315" />
</Popup>
Run Code Online (Sandbox Code Playgroud)
这里我设置了弹出动画属性来滑动.但是当它打开时,它没有动画.我是否必须添加任何其他弹出窗口配置才能打开动画选项幻灯片?
我正在使用.net框架版本3.5.
我有一个WPF ListBox,其中包含我所拥有的特定类的项目的绑定列表.像这样的东西:
ObservableCollection<MyTable> tables = new ObservableCollection<MyTable>();
...
listTables.ItemsSource = tables;
Run Code Online (Sandbox Code Playgroud)
和XAML:
<ListBox HorizontalAlignment="Left" Margin="8,10,0,0" Name="listTables" Width="153" ItemsSource="{Binding tables}" SelectionChanged="listTables_SelectionChanged" Height="501" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1">
<TextBlock Grid.Column="1" Text="{Binding tableName}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
一切正常.我现在要做的是对ListBox中的每个项目有不同的背景,具体取决于类的某个属性.例如,假设MyTable类有一个名为isOccupied的属性.如果为某个项目设置了这个标志,我希望它在ListBox中有一个红色背景,如果不是,那么我想让它具有绿色背景.如果属性发生更改,则背景应相应更改.
有关如何实现这一目标的任何提示?我现在正在查找有关ItemContainerStyle的一些信息,但我对此比较陌生,所以我不确定我是否遵循了正确的道路.
Out团队希望创建可重用,可管理的视图.例如,我们希望在不同的应用程序中重用CommonPromptView
(我们自己的,可自定义的对话框,我们可以隐藏"取消"按钮,设置标题,显示特定图标等).
该视图在其表面上有几个元素:TextBlocks,Buttons.我们想让它们成为一种风格.
那么,解决这个任务的最佳方法是什么?
在第一种情况下,可以通过两种方式支持样式:
两者都不干净(在我看来).
但是如果View是UserControl,那么每次创建新app的人都必须创建一个新窗口来包含UserControl并将其绑定到DP(Style类型)正确.此外,如果UserControl拥有它自己非常方便的API(最常用操作的静态方法),对于包含UserControl的Window用户来说,这将丢失.
Update
CommonPromptView
作为UserControl实现的示例.
Code-behind
public sealed partial class CommonPromptView {
private const int CloseViewTimeIntervalInMilliseconds = 120000;
private DispatcherTimer timer;
public static readonly DependencyProperty CommonPromptBorderStyleProperty = DependencyProperty.Register(
"CommonPromptBorderStyle", typeof (Style), typeof (CommonPromptView), new PropertyMetadata(default(Style)));
public Style CommonPromptBorderStyle {
get { return (Style) GetValue(CommonPromptBorderStyleProperty); }
set { SetValue(CommonPromptBorderStyleProperty, value); }
}
public static readonly DependencyProperty CommonPromptHeaderStyleProperty = DependencyProperty.Register(
"CommonPromptHeaderStyle", typeof (Style), typeof (CommonPromptView), new PropertyMetadata(default(Style)));
public Style CommonPromptHeaderStyle {
get …
Run Code Online (Sandbox Code Playgroud) xaml ×10
wpf ×7
c# ×6
.net ×2
winrt-xaml ×2
wpf-controls ×2
combobox ×1
data-binding ×1
fullscreen ×1
listbox ×1
selecteditem ×1
windows-8.1 ×1
wpf-4.0 ×1