我试图第一次使用MVVM模式.所以我ItemsControl填充了我的viewmodel对象,使用DataTemplate's 显示; 对象是"节点"和"边缘",DataTemplate用Thumb和Polyline对象表示,我希望能够检测到点击和拖动ItemsControl,以便移动节点和边缘.
两个问题:
Polyline's和Thumb?(我可以将一个Thumb.DragDelta处理程序附加到ItemsControl并e.OriginalSource指向Thumb,但是如何获取相应的viewmodel对象?)ItemsControl到检测鼠标单击和拖动空白区域?(答案如下)注意:我知道如果它直接处理View的事件,它可能不被认为是正确的ViewModel.但重要的是,我需要处理鼠标事件,我不知道如何附加它们.
我有一个使用StackPanel的ItemsControl来显示项目列表.
我想为每一行显示一个标签,但是对于要由DataTemplateSelector定义的标签左侧的内容.我不想重新定义TemplateSelector生成的每个DataTemplate的标签.
这可能吗?
<ItemsControl ItemsSource="{Binding Path=Values}" >
<ItemsControl.Resources>
<v:MyTemplateSelector x:Key="myTemplateSelector"></v:MyTemplateSelector>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Label>Test: </Label>
<!--What goes here should be defined by myTemplateSelector-->
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud) 我有一个我在运行时构造的listView,即在编译时不知道列.
我想将DataTemplate应用于单元格,使TextAlignment属性为TextAlignment.Right.创建列时:
foreach (var col in dataMatrix.Columns)
{
gridView.Columns.Add(
new GridViewColumn
{
Header = col.Name,
DisplayMemberBinding = new Binding(string.Format("[{0}]", count)),
CellTemplate = getDataTemplate(count),
});
count++;
}
private static DataTemplate getDataTemplate(int count)
{
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock));
factory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
template.VisualTree = factory;
return template;
}
Run Code Online (Sandbox Code Playgroud)
上面的示例代码无法正常工作,因为单元格内容仍然与左侧对齐.
我正在尝试在我的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的List,它显示文本,旁边有一个"x"按钮.我希望"X"btn显示在最右边,所以它们都出现在同一个地方.我使用的XML是:
<ListBox Name="seiveListBox" ItemsSource="{Binding}" MinWidth="80" Height="120" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<Button Name="delSeiveFromListBtn" Content="X" ToolTip="Delete" Margin="8, 0, 0, 0" Click="delSeiveFromListBtn_Click"></Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我尝试在StackPanel中添加Grid inpalce,但没有成功.
如何设计它或将列表中的"x"对齐到每个项目的最右侧.
我有我的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.仍有例外情况仍然存在.
提前致谢.
尝试将2个或更多Comboboxes SelectedValue绑定到属性时出现问题,即null.只有1个与此属性绑定的组合框将显示实际值.
下面是我的Xaml,我使用DataTemplate选择一个Combobox来呈现viewModel.
XAML:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:PropertyValueViewModel}">
<ComboBox SelectedValue="{Binding Value}" ItemsSource="{Binding SelectableValues}" DisplayMemberPath="Description" SelectedValuePath="Value"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<Label Content="These uses template:"></Label>
<ContentPresenter Content="{Binding ValueSelector}"></ContentPresenter>
<ContentPresenter Content="{Binding ValueSelector}"></ContentPresenter>
<ContentPresenter Content="{Binding ValueSelector}"></ContentPresenter>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
而背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ValueSelector = new PropertyValueViewModel()
{
SelectableValues = new List<SelectableValue>()
{
new SelectableValue("NULL", null),
new SelectableValue("1", 1)
},
Value = null
};
DataContext = this;
}
public static readonly …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建我的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) 我有一个FlipView控件,其DataTemplate定义如下:
<FlipView x:Name="FlipView5Horizontal" Width="480" Height="270" BorderBrush="Black" BorderThickness="1" Style="{StaticResource FlipViewStyle1}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Width="480" Name="xxxImage" Height="270" Source="{Binding Image}" Stretch="UniformToFill"/>
<Border Name="xxxBorder" Background="#A5000000" Height="80" VerticalAlignment="Bottom">
<TextBlock Name="xxxTB" Text="{Binding Title}" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20"/>
</Border>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
Run Code Online (Sandbox Code Playgroud)
在后面的代码中,我需要访问名为“ xxxTB”的TextBlock。这是我要做的代码:
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return …Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题.我在Xamarin Forms页面上定义了两个datatemplates.当按下按钮时,我想换一个用于另一个.我知道ListView我可以使用ItemTemplate和绑定到我DataTemplateSelector来更改列表中项目的视图.
但我只想通过DataTemplateSelector点击按钮或类似按钮来交换View/StackLayout/Frame之类的东西.但我找不到任何提供ItemTemplate列表的控件ListView.
是否有可以实现此目的的控件?
datatemplate ×10
wpf ×7
c# ×3
xaml ×3
binding ×2
alignment ×1
code-behind ×1
combobox ×1
designer ×1
events ×1
itemtemplate ×1
list ×1
listview ×1
mvvm ×1
silverlight ×1
storagefile ×1
uwp ×1
winrt-xaml ×1
xamarin ×1
xamlreader ×1
xbind ×1