我试图第一次使用MVVM模式.所以我ItemsControl填充了我的viewmodel对象,使用DataTemplate's 显示; 对象是"节点"和"边缘",DataTemplate用Thumb和Polyline对象表示,我希望能够检测到点击和拖动ItemsControl,以便移动节点和边缘.
两个问题:
Polyline's和Thumb?(我可以将一个Thumb.DragDelta处理程序附加到ItemsControl并e.OriginalSource指向Thumb,但是如何获取相应的viewmodel对象?)ItemsControl到检测鼠标单击和拖动空白区域?(答案如下)注意:我知道如果它直接处理View的事件,它可能不被认为是正确的ViewModel.但重要的是,我需要处理鼠标事件,我不知道如何附加它们.
即使在SO上已经存在一些相关问题,我也无法找到解决以下问题的干净解决方案.
如果我有一个多次使用的数据模板,例如TreeViewItem.HeaderTemplate,我怎样才能更改某些TreeViewItems的模板.
例如,假设我的TVI HeaderTemplate有一个文本块,根据字符串,我想使字体粗体.
我想做这样的事情:
((TextBlock)myTreeView.Items.ElementAt(0).FindName("myTextBlock")).FontWeight = FontWeights.Bold;
Run Code Online (Sandbox Code Playgroud)
有人有解决方案吗? - >谢谢埃文
编辑:有没有办法编写泛型函数来获取基于它的名称的控件,即使它在数据模板中?
LayoutRoot.FindName("myTextBlock");如果myTextBlock不在datatemplate中,它将起作用.我怎么写findElementInDataTemplate(string elementName, string parentName)函数?
Evan的回答不是我想要的原因是因为我正在开发一个控件.我希望使用我的控件的应用程序开发人员能够更改控件中的任何元素.如果我使用Evan的解决方案,则需要应用程序开发人员访问控件中的所有模板.可能,但不理想.谢谢!
是否可以在数据模板中显式使用CollectionViewSource?通常我们将CollectionViewSource放在模板旁边的资源中,但我们的模型不允许这样做,因为collectionviewsource的'source'是树中此级别的DataContext的属性,这意味着需要有一个实例在这个水平.把它放在资源的根部就意味着只有一个实例.我们也不能简单地在外层使用分组,因为这些项目不存在,直到您在层次结构中这么远,并且并非所有兄弟都拥有此属性.因此,逻辑上我们在DataTemplate中实例化CollectionViewSource(在这个例子中是HierarchicalDataTemplate,但这是无关紧要的.)
具体来说,我们尝试在此特定节点级别允许特定排序.我们唯一的另一种选择是在ViewModel本身进行排序,但由于我们使用的ObservableCollections本身不支持排序,因此会变得很痛苦.实际上,我们在这个主题上看到的每篇文章都说明你应该正是因为这个原因使用CollectionViewSource,因此这个问题.
例如,这有效......
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemTemplate="{StaticResource TreeViewSymbolTemplate}"
ItemsSource="{Binding Symbols}">
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
但这不......
<HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemTemplate="{StaticResource TreeViewSymbolTemplate}">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source="{Binding Symbols}" />
</Binding.Source>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
在我看来它会像它一样,但事实并非如此.同样,我们不能将CollectionViewSource放在与数据模板相同的级别,因为每个模板需要一个实例,因为每个模板都有自己的一组项目(尽管它们都将共享排序条件.)
中号
data-binding wpf datatemplate observablecollection collectionviewsource
嘿所有,我正在尝试将动画应用到任何添加到WP7的silverlight子集中的datatemplate的元素.我有一些问题.虽然在使用DataTemplate.Triggers的WPF(Animate WPF Datatemplate,当项目添加到Listbox?)时似乎很有可能,但WP7的silverlight没有DataTemplates的Triggers属性.因此,我将如何在winphone上执行此操作?
silverlight listbox datatemplate windows-mobile windows-phone-7
我正在尝试在我的WPF项目中实现下面的代码,以便为具有动态列的DataGrid动态生成DataTemplates.我在这里找到了StackOverflow上的代码
public DataTemplate Create(Type type)
{
return (DataTemplate)XamlReader.Load(
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/client/2007"">
<" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/>
</DataTemplate>"
);
}
Run Code Online (Sandbox Code Playgroud)
但是,在XamlReader.Load代码上,我收到错误"无法从'string'转换为'System.Xaml.XamlReader'.
我试图通过将代码更改为:
return (DataTemplate)XamlReader.Load(XmlReader.Create(
Run Code Online (Sandbox Code Playgroud)
但是我在字符串中传递无效字符时遇到错误.
另外,我不确定如何将TextBlock传递给此代码.我想我会创建一个TextBlock并将其作为Type参数传递,但我得到错误"无法从'System.Windows.Controls.TextBlock'转换为'System.Type'
任何帮助赞赏.
如果将控件放入 DataTemplate 中,为什么它们的各个状态会复制或反映在 TabControl 中的每个选项卡中?你在一个选项卡中更改它,所有其他选项卡都会反映,这是为什么?!在我看来,TabControl 仅初始化一个模板化的 ContentControl,并且每次单击选项卡都会重新复制其中的整个内容 - 使旧的控件状态保持不变。要明白我的意思,请考虑将其放入您的 XAML-Pad 中:
<TabControl>
<TabControl.ContentTemplate>
<DataTemplate>
<Border>
<TextBox Text="test"/>
</Border>
</DataTemplate>
</TabControl.ContentTemplate>
<TabItem Header="Tab1"/>
<TabItem Header="Tab2"/>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
它将创建一个带有两个模板化选项卡的 TabControl。现在在文本框中输入一些内容并切换到另一个选项卡,输入的文本将保留。现在每个选项卡将具有相同的内容。我没有在 ListBox 或任何其他控件中观察到相同的行为,这使得实际工作非常困难,因为每一点都需要绑定到 ViewModel 才能使其在 TabControl 中可用。当我在 DataTemplate 中使用的扩展器在我的所有选项卡中弹出时,我注意到了这种奇怪的行为,尽管我专门解决了一个问题。作为一种解决方法,我必须将“IsExpanded”绑定到 ViewModel 中的属性,但这样做确实很糟糕。
有人知道这里发生了什么事吗?
<TabControl x:Name="MainTab" SelectedIndex="0"/>
...
Collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Collection_CollectionChanged);
...
void Collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
TabItem MyNewItem = new TabItem();
ContentPresenter MyContentPresenter = new ContentPresenter();
MyContentPresenter.ContentTemplate = (DataTemplate)this.FindResource("MyTemplate");
MyContentPresenter.Content = e.NewItems[0];
MyNewItem.Content = MyContentPresenter;
MainTab.Items.Add(MyNewItem ); …Run Code Online (Sandbox Code Playgroud) 我有我的GridView使用的图像(PNG)文件作为其DataTemplate的一部分.如果我尝试删除GridView中的特定对象行,我也会删除该行的相关图像文件.列表中的每个项目的图像都不同.
我正在使用此代码删除图像文件
StorageFile _file = await DataStore.GetFileAsync(filename);
await _file.DeleteAsync(StorageDeleteOption.Default);
Run Code Online (Sandbox Code Playgroud)
图像文件在GridView的DataTemplate下的GridView上呈现.所以在我的List中的每个对象模型中,我都有一个公共属性,它为我的DataTemplate返回一个ImageSource.
我正在从List中删除对象行并在GridView刷新新的List项之后立即调用我的删除过程.
即使列表不再包含对象(使用图像),如果我尝试删除文件,应用程序也会抛出Access is Denied异常.当应用程序运行时,如果我尝试手动删除该特定文件(通过文件浏览器),它也不允许我.
我尝试清除我的应用程序中的所有未使用的对象,甚至在删除图像之前将GridView的ItemSource设置为null并将List设置为null.仍有例外情况仍然存在.
提前致谢.
我正在尝试构建我的UWP应用程序,当前尝试使用DataTemplate与x:Bind在资源字典中时,我遇到了设计器异常.
我创建了一个资源字典"ItemTemplates.xaml",其中包含相应的代码隐藏(以确保x:Bind初始化).该文件只包含一个模板:
<DataTemplate x:Key="HomeViewCategoryListItemTemplate" x:DataType="models:Category">
<Button Background="#88333333" Height="110" VerticalContentAlignment="Top" Padding="10" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Light" HorizontalAlignment="Center" Text="{x:Bind Name}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" />
<TextBlock Foreground="{ThemeResource ToolTipForegroundThemeBrush}" HorizontalAlignment="Center" Margin="0,10,0,0" Text="{x:Bind Description}" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" />
</Grid>
</Button>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
然后我将这个资源字典添加到App.xaml,如下所示:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Resources/Core.xaml" />
<resources:ItemTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
现在项目无法使用,因为设计师抛出奇怪的异常,但是当我清理并重建项目并导航到HomeView.xaml页面时,设计器只显示默认的"ToString()"项(基本上列表视图只包含三次) ListView中的文本"Models.Categories"和ListView的ItemTemplate属性带有下划线并显示以下错误:
The resource "HomeViewCategoryListItemTemplate" could not be resolved.
Run Code Online (Sandbox Code Playgroud)
当我导航回App.xaml时,我看到另一个下划线(该<resources:ItemTemplates />行):
The property 'DataType' was not found in type 'DataTemplate'.
Run Code Online (Sandbox Code Playgroud)
这两个错误都是非敏感的,因为当我实际运行应用程序时,没有任何问题,一切都运行良好.到目前为止,我发现的唯一解决方法是以经典方式和"编译"方式包括ResourceDictionary两次:
<ResourceDictionary …Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题.我在Xamarin Forms页面上定义了两个datatemplates.当按下按钮时,我想换一个用于另一个.我知道ListView我可以使用ItemTemplate和绑定到我DataTemplateSelector来更改列表中项目的视图.
但我只想通过DataTemplateSelector点击按钮或类似按钮来交换View/StackLayout/Frame之类的东西.但我找不到任何提供ItemTemplate列表的控件ListView.
是否有可以实现此目的的控件?
我有一个带有单个特定选项卡的 TabControl 和一个绑定到一组 VM 的集合,使用不同的用户控件。为此,我使用在控件资源中定义的 CompositeCollection 和 DataTemplates,根据 VM 类型(充当 ContentTemplate)选择正确的用户控件。
我还设置了一个 ItemTemplate 来定义带有绑定的选项卡项的名称,但它没有在资源中定义,因为我猜它会与“ContentTemplate”冲突。
它工作正常,但我看到跟踪到以下错误:
System.Windows.Data 错误:26:ItemTemplate 和 ItemTemplateSelector 对于已经属于 ItemsControl 的容器类型的项目被忽略;类型='标签项'
看起来 ContentTemplate 和 ItemTemplate 之间存在一些冲突,但我不知道如何解决?
代码如下:
<TabControl HorizontalAlignment="Left" Height="300" Width="500">
<TabControl.Resources>
<CollectionViewSource x:Key="personCollection" Source="{Binding Persons}" />
<DataTemplate DataType="{x:Type viewModel:Main}">
<local:MainView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:Person}">
<local:PersonView />
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemsSource>
<CompositeCollection>
<TabItem Header="General" Content="{Binding }"/>
<CollectionContainer Collection="{Binding Source={StaticResource personCollection}}" />
</CompositeCollection>
</TabControl.ItemsSource>
<TabControl.ItemTemplate>
<DataTemplate DataType="viewModel:Person">
<TextBlock Text="{Binding FirstName}" />
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
Run Code Online (Sandbox Code Playgroud) datatemplate ×10
wpf ×5
silverlight ×4
xaml ×4
c# ×2
binding ×1
data-binding ×1
designer ×1
events ×1
listbox ×1
mvvm ×1
storagefile ×1
tabcontrol ×1
uwp ×1
winrt-xaml ×1
xamarin ×1
xamlreader ×1
xbind ×1