c# - WPF 和 MVVM : How use attached properties in my UserControl

标签 c# mvvm properties grid

最近我一直在尝试制作一个网格,使用我的 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>

提前致谢, 尼克·范德梅杰

最佳答案

根据发现的知识让它工作 herehere .它并不漂亮,但这似乎是唯一可行的方法。

您没有共享 MainViewModel(我猜是因为您提供的快速示例没有必要)。但是由于我需要它来进行绑定(bind)并且您很可能会使用它,所以这是我的快速版本:

class MainViewModel
{
    public List<BlockViewModel> Blocks { get; set; }

    public MainViewModel()
    {
        Blocks = new List<BlockViewModel> { new BlockViewModel() };
    }
}

您需要将网格包裹在 ItemsControl 中,并使用 ItemContainerStyle 绑定(bind) Grid.ColumnGrid.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/

相关文章:

C# 如何根据多个范围索引生成新字符串

c# - Items控制可点击的项目

validation - 如何在 ItemsControl 内的 TextBox 上使用 ValidatesOnDataErrors

MVVM 依赖注入(inject)跨平台

c# - 为什么我不能将 set 访问器添加到覆盖属性?

c# - 检测 .NET Standard 2.1 中的异常上下文

c# - 我可以将一个大的 dll 分成每个类 1 个 dll 吗?

wpf - 是否有任何MVVM库实现CommandSinkBinding?

iphone - Objective-C 中的非原子和只读属性

java - SpringBootApplication无法识别属性配置