2016-10-03 45 views
1

我正在嘗試構建一個簡單的應用程序,該應用程序具有一個用於定義玩家名稱和樣式的控件。該控件可用於玩家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> 

主窗口是一個簡單的網格定義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> 

最後,有MainWindowViewModel,我需要中繼的數據,這是由PlayInfoControl抓獲。

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

我第一次嘗試解決設置組。我創建了一個自定義的依賴項屬性,但是由於應用程序無法構建,因此我不確定我在做什麼錯誤。

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不能被分配給一個字符串。更換

new PropertyMetadata(0)); 

new PropertyMetadata(string.Empty)); 
+0

MainWindow.xaml被抱怨 – haosmark

+0

「玩家類型必須自DependencyObject派生」我伸出球員:DependencyObject的和組現在工作正常!如何將我使用Player.cs收集的數據傳遞給MainWindowViewModel.cs? – haosmark

相關問題