2013-07-19 67 views
1

我遇到了绑定变量颜色的问题,这是我在类中定义的,以便它可以为我填充区域。Windows phone将xaml绑定到类

<UserControl x:Name="Portugal" x:Class="MiddleAgeWar1.UserControl_Portugal" 
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:country="clr-namespace:MiddleAgeWar1.ViewModel" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
d:DesignHeight="152" d:DesignWidth="88"> 

<Grid x:Name="LayoutRoot"> 
    <Path x:Name="Path" Stretch="Fill" Fill="{Binding color}" StrokeThickness="1.34983" StrokeLineJoin="Round" Stroke="#D5000000" Data="F1 M 43.4622,0.674866C 43.4622,0.674866 42.2811,4.3869 42.1967,4.97748C 42.1124,5.56805 41.8593,5.56805 41.5218,6.07428C 41.1844,6.58038 41.5218,6.74921 41.0156,8.43646C 40.5094,10.1236 41.0156,9.44879 41.0156,10.883C 41.0156,12.3171 40.9313,12.1485 40.9313,12.1485L 42.028,14.7639C 42.028,14.7639 42.7873,17.7165 42.5342,19.1507C 42.2811,20.5849 41.8593,21.5974 41.2687,22.1035C 40.6782,22.6097 40.2563,23.7065 39.6658,24.0439C 39.0752,24.3813 38.7378,25.0562 37.3036,26.0687C 35.8694,27.081 36.713,27.8403 36.2912,28.8526C 35.8694,29.8649 36.1225,30.2868 35.8694,30.9617C 35.6163,31.6367 35.8694,34.252 35.8694,34.5894C 35.8694,34.9269 34.7727,38.0484 34.7727,38.0484C 34.7727,38.0484 34.5196,38.3858 33.001,40.6636C 31.4824,42.9415 32.3261,41.0855 31.145,41.9291C 29.9639,42.7728 29.7952,42.6041 28.6141,43.3633C 27.433,44.1226 28.1922,44.9662 28.0235,46.5692C 27.8548,48.1721 27.686,48.1721 27.686,49.0158C 27.686,49.8594 27.686,50.9561 27.686,52.3903C 27.686,53.8244 27.2642,54.3307 26.758,55.2587C 26.2519,56.1867 26.3362,55.9337 25.83,56.3555C 25.3238,56.7773 25.3238,56.6929 24.3115,57.1992C 23.2991,57.7053 23.6365,57.2835 23.2147,57.5366C 22.7929,57.7896 22.6242,58.2115 20.0089,61.3329C 17.3936,64.4544 19.1652,62.5141 18.659,63.4421C 18.1529,64.3701 18.406,64.0327 17.8998,65.045C 17.3936,66.0573 17.3936,66.1418 16.9718,66.9011C 16.5499,67.6602 16.5499,67.3228 15.5376,68.0821C 14.5252,68.8414 15.3688,68.6727 15.0314,69.1789C 14.6939,69.685 14.3565,69.6006 12.4161,71.0349C 10.4757,72.4691 11.8255,71.8785 11.0662,72.3847C 10.307,72.8908 10.307,72.8909 8.53531,73.5659C 6.76366,74.2407 6.84802,75.8437 6.84802,75.8437L 3.97963,78.2059C 2.79853,79.1339 3.64217,79.0495 3.30471,79.8931C 2.96726,80.7368 2.46107,82.7615 1.61742,83.6052C 0.773778,84.4488 1.53306,84.955 1.27997,85.5456C 1.02687,86.1362 0.183228,90.1013 1.02687,90.4387C 1.87052,90.7761 2.88289,90.6075 6.08874,90.3543C 9.29459,90.1013 6.84802,90.3543 9.04149,89.9326C 11.235,89.5107 10.5601,89.5107 11.6568,89.1733C 12.7535,88.8358 12.0786,89.1733 12.4161,89.342C 12.7535,89.5107 14.4408,91.7885 14.4408,92.5478C 14.4408,93.3071 14.4408,93.3914 14.1877,94.0663C 13.9346,94.7413 13.9346,94.7413 13.4284,95.1631C 12.9223,95.5849 12.2473,95.7537 11.4881,95.9224C 10.7288,96.091 9.9695,96.4285 8.61967,96.8504C 7.26985,97.2722 8.02912,97.1035 7.35421,97.3566C 6.67929,97.6097 7.10111,97.5253 4.73891,98.7908C 2.37671,100.056 4.73891,99.2126 4.73891,100.056C 4.73891,100.9 5.49819,100.9 6.00438,101.237C 6.51056,101.575 8.53531,101.912 10.3913,102.165C 12.2473,102.418 14.5252,102.587 14.5252,102.587L 15.875,103.768L 15.4532,106.89C 15.4532,108.493 15.4532,109.252 15.2845,111.445C 15.1157,113.639 14.947,112.373 14.4408,113.301C 13.9346,114.229 13.9346,114.483 13.091,116.254C 12.2473,118.026 12.7535,117.182 11.1506,119.46C 9.54769,121.738 10.307,121.569 10.0539,123.594C 9.80078,125.619 9.54768,124.353 8.19785,126.04C 6.84802,127.728 7.10111,127.559 6.34183,129.162C 5.58255,130.765 5.75128,129.921 4.48582,132.115C 3.22035,134.308 4.06399,133.043 3.64217,133.971C 3.22035,134.899 3.30471,135.996 3.22035,138.02L 3.97963,141.142C 4.82327,142.238 6.08874,143.166 8.70404,143.673C 11.3193,144.179 10.2226,144.601 11.6568,144.938C 13.091,145.276 13.5128,146.035 15.0314,146.71C 16.5499,147.385 16.2125,147.553 17.1405,147.722C 18.0685,147.891 18.9121,148.397 19.5871,148.482C 20.262,148.566 21.6962,148.903 22.5398,148.903C 23.3835,148.903 24.3115,148.819 24.3115,148.819L 28.9515,148.144L 31.2997,148.263C 31.2997,148.263 32.7128,148.179 33.2822,148.158C 33.8517,148.137 34.6532,148.137 36.0873,148.032C 37.5215,147.926 37.8801,147.926 38.6816,148.032C 39.483,148.137 41.8452,148.411 41.8452,148.411L 45.1354,148.924L 49.6958,151.635C 49.6958,151.635 49.4708,140.162 50.8207,130.713C 52.1705,121.264 46.9962,119.464 59.8196,106.866C 72.643,94.2674 73.7678,89.768 74.8927,87.0684C 76.0175,84.3687 75.7926,67.4958 76.0175,60.7466C 76.2425,53.9975 81.9441,38.369 83.2096,36.2598C 84.4751,34.1507 86.5842,28.0765 87.6809,19.1341C 88.7777,10.1916 66.5898,3.52667 65.24,2.7674C 63.8901,2.008 51.2355,3.18915 50.4762,2.34552C 49.7169,1.50189 43.5021,0.770813 43.4622,0.674866 Z " Margin="0,-0.001,-0.409,-0.309" UseLayoutRounding="False" /> 
    <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="88"/> 

</Grid> 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Media; 

namespace MiddleAgeWar1.ViewModel 
{ 
    class Country 
    { 
     public SolidColorBrush color = new SolidColorBrush(Colors.Green); 
    } 
} 

我希望绑定的SolidColorBrush的颜色,使其充满我的领域。

+0

如果您的UserControl_Portugal控件将它的DataContext设置为“Country”的一个实例,它应该可以工作。 (例如:DataContext = new Country();) –

+0

@ jonasN89你有没有找到其他解决方案? – loop

回答

1

我认为你应该重新安排你的代码,并开始使用MVVM。 您可以很容易地通过MVVM使用它。

转到Nuget并搜索MVVM或Galsoft。

在模型中,你可以定义你想要的。 然后你可以创建你的国家作为一个视图。 然后绑定很容易在ViewModel中完成。

这将需要一些时间来进入它。但是当你做到一切都很简单!

+0

我也发现了这是前进的方向,所以我开始一个新的,看看它是否会奏效。 – JonasN89

0

将其绑定到您的网格这样的..

<Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="88" Background="{Binding color}"/> 

,并在你的类..它实现InotifyPropetyChanged好做...你可以做这样的..

在你的班级做这个..

class Country:INotifyPropertyChanged 
{ 


public Country() 
    { 
     this.InitializeComponent(); 
this.DataContext = this; 
} 

private SolidColorBrush _color; 

    public SolidColorBrush color 
    { 
     get 
     { 
      return _color; 
     } 
     set 
     { 
      _color = value; 
      FirePropertyChanged("color"); 
     } 

    } 

public event PropertyChangedEventHandler PropertyChanged; 
    protected void FirePropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

} 
+0

谢谢,但似乎有InitializeComponent和DataContext的问题。这是因为我错过了一些代码或? – JonasN89

+0

看@ JonasN89你可以使用绑定只有当变量被定义为像属性..你的颜色没有任何获得(这将在xaml中绑定时使用颜色时调用),所以你没有得到你的颜色..我的方式以为你是纯粹的基本..所以刚刚阅读数据绑定的基础知识,那么你会发现我告诉你.. – loop

+0

只是检查任何基本的xaml数据绑定的例子..在谷歌.. – loop