自定义属性和视图模型之间的数据通信

时间:2016-10-03 17:03:26

标签: c# wpf xaml

我正在尝试构建一个简单的应用程序,该应用程序具有定义玩家姓名和风格的控件。该控件重复用于玩家1和玩家2.第一个目标是设置两个单选按钮组,以便每个玩家相应地进行选择。第二种是将来自该控件的数据传递给主页面的视图模型。我如何完成这些事情?

这是控件的样子:

enter image description here

以下是项目结构:

enter image description here

Player.cs只是引用了玩家的风格及其名称

namespace temp
{
    class Player
    {
        // offensive / defensive
        public string Style { get; set; }

        public string Name { get; set; }
    }
}

它还充当PlayerInfoControl的视图模型。这是代码:

<UserControl x:Class="temp.Controls.PersonInfoControl"
             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" 
             xmlns:local="clr-namespace:temp.Controls"
             xmlns:vm="clr-namespace:temp"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <vm:Player x:Key="ViewModel" />
    </UserControl.Resources>

    <Grid DataContext="{StaticResource ViewModel}">
        <StackPanel Orientation="Horizontal" Height="50">
            <StackPanel Margin="0,0,15,0" VerticalAlignment="Center">
                <RadioButton Content="Offense" GroupName="{Binding Group}" />
                <RadioButton Content="Defense" GroupName="{Binding Group}" />
            </StackPanel>
            <StackPanel Margin="0,0,15,0" Orientation="Horizontal" VerticalAlignment="Center">
                <Label Content="Name" />
                <TextBox Width="100" Height="25" Text="{Binding Name}"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

MainWindow是一个简单的网格,用于定义playerInfoControl两次

<Window x:Class="temp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:temp"
        xmlns:controls="clr-namespace:temp.Controls"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <local:MainWindowViewModel x:Key="ViewModel" />
    </Window.Resources>

    <Grid DataContext="{StaticResource ViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="160" />
        </Grid.RowDefinitions>

        <controls:PersonInfoControl Grid.Row="0" Group="p1" />
        <controls:PersonInfoControl Grid.Row="1" Group="p2" />
    </Grid>
</Window>

最后,我需要将数据转发给PlayInfoControl捕获的MainWindowViewModel。

namespace temp
{
    class MainWindowViewModel
    {
        Player Player1 = new Player();
        Player Player2 = new Player();
    }
}

我首先尝试解决设置群组的问题。我创建了一个自定义依赖项属性,但由于app而无法构建,我不确定我做错了什么。

public partial class PersonInfoControl : UserControl
    {
        public PersonInfoControl()
        {
            InitializeComponent();
        }

        public string Group
        {
            get { return (string)GetValue(GroupProperty); }
            set { SetValue(GroupProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Group.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GroupProperty =
            DependencyProperty.Register("Group", typeof(string), typeof(Player), new PropertyMetadata(0));
    }

这就是错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要使用正确的类型作为依赖项属性的默认值。在您的情况下,其0无法分配给字符串。替换

new PropertyMetadata(0));

new PropertyMetadata(string.Empty));