我有一个与此相关的问题:我正在尝试将事件附加到我的StackPanel,但在使用XamlReader时似乎没有连接.我无法调用ChildItem_Load方法.有谁知道这样做的方法?
除了这个事件,代码工作正常.
this._listBox.ItemTemplate = (DataTemplate) XamlReader.Load(
@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Border>
<StackPanel Loaded=""ChildItem_Loaded"">
<TextBlock Text=""{Binding " + this._displayMemberPath + @"}"" />
</StackPanel>
</Border>
</DataTemplate>"
Run Code Online (Sandbox Code Playgroud) <CombobBox x:Name="cbo"
Style="{StaticResource ComboStyle1}"
DisplayMemberPath="NAME"
SelectedItem="{Binding Path=NAME}"
SelectedIndex="1">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=NAME}"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
在Window OnLoaded事件中,我编写了代码来设置ItemsSource:
cbo.ItemsSource = ser.GetCity().DefaultView;
Run Code Online (Sandbox Code Playgroud)
在加载窗口时,我可以看到最初正在加载第一个项目,但同时它清除了显示的项目。我被困在这种情况下,任何帮助表示赞赏。
问候
基肖尔
我正在使用Microsoft提供的WPF功能区控件.
问题在于,当我使用DataTemplate填充a时RibbonApplicationSplitMenuItem,我得到一个额外的嵌套级别,我认为不应该存在.
以下是相关的WPF代码:
<Window.Resources>
<DataTemplate DataType="{x:Type cfg:PluginInfoConfigurationElement}" x:Key="GotoPluginAppMenuItem">
<ribbon:RibbonApplicationMenuItem
Header="{Binding Path=Key}"
ImageSource="{Binding Path=Image}"/>
</DataTemplate>
</Window.Resources>
<ribbon:RibbonApplicationMenu>
<ribbon:RibbonApplicationSplitMenuItem x:Name="LoadPluginMenuItem"
ItemsSource="{Binding Source={StaticResource NlpModel}, Path=AvailablePlugins}"
Header="Plugins"
ItemTemplate="{StaticResource GotoPluginAppMenuItem}">
</ribbon:RibbonApplicationSplitMenuItem>
<ribbon:RibbonApplicationSplitMenuItem x:Name="LoadPluginMenuItem2"
Header="Plugins">
<ribbon:RibbonApplicationMenuItem
Header="FooPlugin"
ImageSource="Images/icon-32.png"/>
<ribbon:RibbonApplicationMenuItem
Header="Invalid"
ImageSource="Images/icon-32.png"/>
</ribbon:RibbonApplicationSplitMenuItem>
<!-- Other items to fill the menu -->
</ribbon:RibbonApplicationMenu>
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
使用DataTemplate http://img571.imageshack.us/img571/9915/screentemplate.png 使用数据模板.
我想要什么http://img43.imageshack.us/img43/9168/screendesired.png 没有模板.
如您所见,使用DataTemplate时会出现额外的嵌套级别.我怎么能防止这种情况?
我无法编写xaml表示以允许绑定到我的后台ViewModel以进行级联菜单
这是VM:
public class MenuNode
{
public string Header {get;}
public List<MenuNode> Items {get;}
}
Run Code Online (Sandbox Code Playgroud)
我有的xaml是这样的:
<ContextMenu ItemsSource="{Binding Choices}" >
<ContextMenu.Resources>
<DataTemplate DataType="{x:Type vmi:MenuNode}">
<MenuItem Header="{Binding Header}" ItemsSource="{Binding Items}"/>
</DataTemplate>
</ContextMenu.Resources>
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
当菜单弹出时,我得到带箭头的第一级条目(表示应该有一个子菜单),但当我将鼠标悬停在菜单上时,它不会显示子菜单项.
有任何想法吗?
我有一个带有ItemsControl的WPF-View,它从ViewModel绑定ObservableCollection.现在我想逐渐淡出我从ObservableCollection中删除的项目.
视图模型:
public class ViewModel
{
public ObservableCollection<string> Items { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图:
<Window x:Class="Sandbox.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"
Name="mainWindow">
<Window.Resources>
<DataTemplate x:Key="mytemplate">
<DataTemplate.Resources>
<Storyboard x:Key="OnUnloaded">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="-0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="-10"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</DataTemplate.Resources>
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<TextBox Text="{Binding Mode=OneWay}"/>
</Grid>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Unloaded">
<BeginStoryboard Storyboard="{StaticResource OnUnloaded}"/>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ItemsControl Grid.Row="1" ItemsSource="{Binding …Run Code Online (Sandbox Code Playgroud) 对此已有疑问,但他们没有回答我的问题.例如:
<ContentControl.Resources>
<DataTemplate DataType="{x:Type Databinding:RedScreenViewModel}" >
<Databinding:RedScreen DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type Databinding:BlueScreenViewModel}">
<Databinding:BlueScreen DataContext="{Binding}" />
</DataTemplate>
</ContentControl.Resources>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在尝试为创建的View设置DataContext,希望它将使用用于创建它的ViewModel.但是当我单步执行代码时,DataContext始终为null.我怎样才能实现我的目标?谢谢
如以下文章http://vbcity.com/blogs/xtab/archive/2009/12/15/wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx中所述,我使用VirtualizingStackPanel来改进我的项目的ComboBoxes的性能.
并且效果很好......直到我将一个样式应用于ComboBox来更改布局(我在这里找到了样式:http://msdn.microsoft.com/en-us/library/ee230084.aspx)
以下是演示此事的样本的源代码(确定ComboBox的ItemSource属性填充了10000个项目).
如果有人有想法......
<Window.Resources>
<!-- Fill Brushes -->
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFF" Offset="0.0"/>
<GradientStop Color="#CCC" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<LinearGradientBrush x:Key="HorizontalNormalBrush" StartPoint="0,0" EndPoint="1,0">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFF" Offset="0.0"/>
<GradientStop Color="#CCC" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFF" Offset="0.0"/>
<GradientStop Color="#EEE" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<LinearGradientBrush x:Key="HorizontalLightBrush" StartPoint="0,0" EndPoint="1,0">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFF" Offset="0.0"/>
<GradientStop Color="#EEE" Offset="1.0"/> …Run Code Online (Sandbox Code Playgroud) 我需要在树视图中显示Hierarchy .但是细节应该显示在数据网格中.

我如何编写模板来实现这一目标?我现在误解模板中的smth.
<TreeView ItemsSource="{Binding Path=Categories}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type stackProjects:Category}" ItemsSource="{Binding Path=SubCategories}">
<TextBlock Margin="3" Text="{Binding Path=CategoryName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type stackProjects:SubCategory}" ItemsSource="{Binding Path=Details}">
<TextBlock Text="{Binding Path=SubCategoryName}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type stackProjects:Detail}" >
<StackPanel Orientation="Horizontal">
<TextBlock Margin="3" Text="{Binding Path=Name}"/>
<TextBlock Margin="3" Text=" - "/>
<TextBlock Margin="3" Text="{Binding Path=Info}"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Run Code Online (Sandbox Code Playgroud) 我搜索一个选项,用c#代码构建一个datatemplate.我用过:
DataTemplate dt = new DataTemplate(typeof(TextBox));
Binding bind = new Binding();
bind.Path = new PropertyPath("Text");
bind.Mode = BindingMode.TwoWay;
FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBox));
txtElement.SetBinding(TextBox.TextProperty, bind);
txtElement.SetValue(TextBox.TextProperty, "test");
dt.VisualTree = txtElement;
textBox1.Resources.Add(dt, null);
Run Code Online (Sandbox Code Playgroud)
但它不起作用(它被放置在窗口的Loaded-Method中 - 所以我的文本框应该在窗口开始时显示"test"一词).任何的想法?
在我的UWP应用程序中,我正在考虑将我的数据模板提取到一个单独的用户控件中,即:<DataTemplate><local:CustomTemplate/></DataTemplate>
并且用户控件(customtemplate)将具有之前在我的DataTemplate中的堆栈面板或网格,以及它的绑定,我已经知道如何为达到这个.
我的问题是,通过在用户控件中提取数据模板,这是否会导致任何性能损失?我在某处读到,在执行此操作时,每个GridViewItem继续执行用户控件的InitializeComponent(),并在UI上解析xaml,这会导致性能问题?但是,如果我们将数据模板保存在同一文件中(未在用户控件中提取),则不会出现性能问题.这是真的 ?
datatemplate ×10
wpf ×7
c# ×3
xaml ×3
combobox ×2
.net ×1
datacontext ×1
datagrid ×1
eventtrigger ×1
mvvm ×1
performance ×1
ribbon ×1
styles ×1
treeview ×1
uwp ×1