这可能不是最干净的解决方案,但是这样的解决方案对您有用吗(显然您会将几何初始化移出构造函数)?
您可以创建您的建议Dictionary<CapPattern,Path>
对象并用您的路径信息填充它,但也应用Transform
to the Geometry
给它你想要的尺寸/偏移量,相对于Canvas
.
public partial class Horses : UserControl, INotifyPropertyChanged
{
public enum CapPattern { ChevronPattern, SomeOtherPattern };
public Dictionary<CapPattern, Geometry> Patterns { get; set; }
private Geometry currentPath;
public Geometry CurrentPath
{
get { return this.currentPath; }
set
{
this.currentPath = value;
NotifyPropertyChanged();
}
}
public Horses()
{
Patterns = new Dictionary<CapPattern, Geometry>();
Patterns.Add(
CapPattern.ChevronPattern,
Geometry.Combine(
Geometry.Parse("M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 17.000006,113.32909 0.5,126.75 3.2500048,108.125 z M212.418,93.416999 L230.918,106.79199 233.668,125.41701 217.168,111.99609 203.543,132.292 200.168,116.32917 z M32.25,48.374999 L44.250004,72.249999 40.625004,90.249999 28.000003,68.581336 7.750001,82.249999 11.665709,64.166339 z M201.918,47.041991 L222.50229,62.833335 226.418,80.916991 206.168,67.248336 193.543,88.916999 189.918,70.916991 z M41,1.8329993 L55.000002,28.166337 51.66667,45.832999 37.333336,23.499837 16.666001,37.417269 21.66571,19.418135 z M193.168,0.5 L212.50229,18.085143 217.502,36.084262 196.83467,22.166836 182.50133,44.499991 179.168,26.833333 z"),
Geometry.Empty,
GeometryCombineMode.Union,
new TranslateTransform(0, 0)));
Patterns.Add(
CapPattern.SomeOtherPattern,
Geometry.Combine(
Geometry.Parse("M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 17.000006,113.32909 0.5,126.75 3.2500048,108.125 z M212.418,93.416999 L230.918,106.79199 233.668,125.41701 217.168,111.99609 203.543,132.292 200.168,116.32917 z M32.25,48.374999 L44.250004,72.249999 40.625004,90.249999 28.000003,68.581336 7.750001,82.249999 11.665709,64.166339 z M201.918,47.041991 L222.50229,62.833335 226.418,80.916991 206.168,67.248336 193.543,88.916999 189.918,70.916991 z M41,1.8329993 L55.000002,28.166337 51.66667,45.832999 37.333336,23.499837 16.666001,37.417269 21.66571,19.418135 z M193.168,0.5 L212.50229,18.085143 217.502,36.084262 196.83467,22.166836 182.50133,44.499991 179.168,26.833333 z"),
Geometry.Empty,
GeometryCombineMode.Union,
new TranslateTransform(20, 30)));
InitializeComponent();
}
// INotifyPropertyChanged implementaton.
}
在我的模型中,我填充了ComboBox
从设置属性的字典中CurrentPath
,这绑定到一个Path
on the Canvas
:
<Grid>
<StackPanel>
<ComboBox ItemsSource="{Binding Path=Patterns}"
SelectedValue="{Binding Path=CurrentPath}"
SelectedValuePath="Value"
DisplayMemberPath="Key"/>
<Canvas>
<Path Data="{Binding Path=CurrentPath}" Stroke="Black" StrokeThickness="1" />
</Canvas>
</StackPanel>
</Grid>
您将保留您的绑定Fill
和其他属性。
另一种方法可能是制作一个小Class
其中持有你的Path
信息以及所需的信息Top
, Left
, Transform
或定位图案所需的任何其他信息。然后,您可以将这些对象的列表绑定到ComboBox
以与上面类似的方式,并将所有必需的属性绑定在Canvas
and Path
到当前选定对象的属性。
Edit:
您还可以在中配置您的转换ResourceDictionary
沿着这些思路:
<Path x:Name="Chevrons" Fill="{Binding SilksModel.BodyColour2, Converter={StaticResource DBColourToColorConverter}, ElementName=root" Data="M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 17.000006,113.32909 0.5,126.75 3.2500048,108.125 z M212.418,93.416999 L230.918,106.79199 233.668,125.41701 217.168,111.99609 203.543,132.292 200.168,116.32917 z M32.25,48.374999 L44.250004,72.249999 40.625004,90.249999 28.000003,68.581336 7.750001,82.249999 11.665709,64.166339 z M201.918,47.041991 L222.50229,62.833335 226.418,80.916991 206.168,67.248336 193.543,88.916999 189.918,70.916991 z M41,1.8329993 L55.000002,28.166337 51.66667,45.832999 37.333336,23.499837 16.666001,37.417269 21.66571,19.418135 z M193.168,0.5 L212.50229,18.085143 217.502,36.084262 196.83467,22.166836 182.50133,44.499991 179.168,26.833333 z" Stroke="Black" StrokeThickness="1">
<Path.RenderTransform>
<TranslateTransform X="20" Y="120"/>
</Path.RenderTransform>
</Path>