- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
最近我一直在尝试制作一个网格,使用我的 UserControls 来填充它。我遇到的唯一问题是我无法使用 WPF 绑定(bind)将用户控件链接到我的网格,这让我很难将它们很好地放置在网格中。我的问题:如何将我的 ViewModel 中的属性(存储在 Block 类中,我的 ViewModel 有一个对象)绑定(bind)到我的 UserControl Block,允许它使用这些变量(X 和 Y,它们是网格的行和列,位置)?
这是我的用户控件的代码:
namespace Mortal_Pets.Views
public partial class BlockView : UserControl
{
public BlockView()
{
InitializeComponent();
DataContext = new BlockViewModel();
}
}
<UserControl x:Class="Mortal_Pets.Views.BlockView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="30">
<Canvas Background="{Binding Block.Color}" Grid.Row="{Binding Path=Block.XPosition}" Grid.Column="{Binding Path=Block.YPosition}">
</Canvas>
这是 UserControl 的 ViewModel 和 Model 类:
class BlockViewModel
{
public Block Block { get; set; }
public BlockViewModel()
{
Block = new Block();
Block.XPosition = 5; //Does Not Work, But this is how i'd like to have it
Block.YPosition = 5; //Does Not Work, But this is how i'd like to have it
Block.Color = new SolidColorBrush(Colors.Black);
}
}
class Block
{
public int XPosition { get; set; }
public int YPosition { get; set; }
public Brush Color { get; set; }
public Block()
{
}
}
然后我们有我的窗口:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BlockView view = new BlockView();
view.SetValue(Grid.ColumnProperty, 5); //This does work, but is ugly and not really usefull for what i intend to do with it
view.SetValue(Grid.RowProperty, 5); //This does work, but is ugly and not really usefull for what i intend to do with it
BoardGrid.Children.Add(view); //This does work, but is ugly and not really usefull for what i intend to do with it
}
}
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Mortal_Pets.Views" x:Class="Mortal_Pets.Views.MainWindow"
Title="MainWindow" Height="700" Width="600"
Loaded="Window_Loaded">
<Grid x:Name="BoardGrid" Margin="10,50,10,10" Width="500" Height="500">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<Label x:Name="ComboCounter" Content="{Binding Game.ComboCounter}" HorizontalAlignment="Left" Margin="-2,-61,0,0" VerticalAlignment="Top" Width="95" Grid.ColumnSpan="4"/>
</Grid>
提前致谢,尼克·范德梅杰
最佳答案
根据发现的知识让它工作 here和 here .它并不漂亮,但这似乎是唯一可行的方法。
您没有共享 MainViewModel
(我猜是因为您提供的快速示例没有必要)。但是由于我需要它来进行绑定(bind)并且您很可能会使用它,所以这是我的快速版本:
class MainViewModel
{
public List<BlockViewModel> Blocks { get; set; }
public MainViewModel()
{
Blocks = new List<BlockViewModel> { new BlockViewModel() };
}
}
您需要将网格包裹在 ItemsControl
中,并使用 ItemContainerStyle
绑定(bind) Grid.Column
和 Grid.Row
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="600"
xmlns:local="clr-namespace:WpfApplication1">
<ItemsControl ItemsSource="{Binding Path=Blocks}">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding Block.YPosition}" />
<Setter Property="Grid.Column" Value="{Binding Block.XPosition}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:BlockView/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Margin="10,50,10,10" Width="500" Height="500" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Window>
BlockView
现在看起来像这样:
<UserControl x:Class="WpfApplication1.BlockView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="30" d:DesignWidth="30">
<Canvas Background="{Binding Block.Color}">
</Canvas>
</UserControl>
关于c# - WPF 和 MVVM : How use attached properties in my UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18980848/
您好,我的 WPF UserControl 知识只有一个小时了。因此,如果有很多关于这个问题的教程或/和答案,请原谅我(老实说,我认为这无法完成,需要重新编写代码……因此我想问的原因) 因此,在创建
我看过几篇文章介绍如何删除在运行时添加的 UserControl,但我的问题有点不同。我有一个 UserControl,它由一个图像组成,右上角有一个小“x”按钮,用于从其父 Canvas 中删除自身
我有一个用户控件 UserControl1,它在其资源中定义了一种样式。该用户控件包含一个 UserControl2 实例,它引用了该样式:
我正在尝试以 How can a WPF UserControl inherit a WPF UserControl? 中提到的方式继承 WPF 中的用户控件 namespace DMS.Presen
我想创建几个具有一些通用功能的 WPF UserControl 类。出于这个原因,我想从一个新的基类派生类,而这个基类又派生自 UserControl。 我的问题是我的 C# 类是 部分 定义的,而自
这个问题在这里已经有了答案: Should a user control have its own view model? (6 个答案) How to correctly bind to a de
我有一个 UserControl,它包含另一个带有 Button 的 UserControl。我想在第一个 UserControl(父级)中向该按钮添加一个事件。我尝试这样做: void Page_I
我需要我所有的用户控件都具有一些类似的功能。所以我的用户控件是: using System.Windows.Controls; namespace WpfApplication26 { ///
以下 WPF UserControl 调用了 DataTypeWholeNumber,它有效。 现在我想创建一个名为 DataTypeDateTime 和 DataTypeEmail 等的 UserC
我有一个包含按钮和其他一些控件的 UserControl: ... 当我创建该控件的新实例时,我想获取 Button 的 Command 属性: 当然,“Th
我不确定这个问题是否已经发布,并且我已经检查了建议的 SO 链接,但似乎没有一个链接与该问题完全相关。因此,如果有人认为这可能是重复的,请发布建议答案的链接并原谅这篇文章。 解决我的问题的方法可能不止
我在 UserControl_1 中有两个 UserControl,有一个按钮可以在 MainWindow.axml 中的 StackPanel 中添加 UserControl_2。我在 UserCo
我正在开发一个由多个传感器组成的模块化项目,每个传感器都继承自一个抽象的 Sensor 类。为了配置每个传感器,我已经开始为每个传感器添加一个用户控制面板(继承自 UserControl),它在运行时
我正在使用 c#.net 我的网络表单中有不同的 View ,除了三个文本框(到达/看到/离开时间)之外,这些通常都显示不同的信息。为了尝试减少代码,我创建了一个包含这三个文本框的 UserContr
我有一个 UserControl,它有两个组件 public System.Windows.Forms.ComboBox innerComboBox; public System.Windows.Fo
我有一个包含 ListBox 的 userControl。我想从另一个 userControl 访问那个 ListBox。 例如: UserControl1.ListBox1.Items.Count;
我在更新另一个用户控件中的用户控件时遇到问题。 示例代码: UserControl MyCart1 = (UserControl)Page.FindControl("MyCart1"); Update
我在下面添加了一些屏幕截图和重现步骤。 我的数据模型有一个基类,我们称它为 CommonThing,它有很多属性。然后有几个具有附加属性的此类的特殊版本。我现在想创建一个 UI 来将数据输入到这个模型
我有一个应用程序,我需要确保在使用 ShowDialog() 单击用户控件上的按钮打开的表单将在我处理用户控件时关闭和处理。 我通过计时器在主窗体中调用 userControl.Dispose()。
我正在努力实现... 用户控件(MyRepeater) Control Start Control End 页面 Page Start Page Item Tem
我是一名优秀的程序员,十分优秀!