Bac*_*con 0 .net c# data-binding wpf xaml
我有一个硬编码的Path形状Canvas.我希望在这个画布中有多个形状,所以我试图将每个形状的信息推送到一个类中,然后使用一个ItemsControl来渲染每个形状.当我使用时ItemsControl,每个形状都位于不正确的位置(太远和向左).
正确显示
<Canvas>
<Path Style="{StaticResource OverlayPath}"
Height="87.934"
Width="96.067"
Canvas.Left="348.456"
Canvas.Top="204.525"
Data="M432.9,245.5 L428.26666,258.46667 439.86716,261.26627 443.46698,246.46662 443.06664,242.33348 437.06651,242.60046 428.26633,239.13402 429.9994,232.33489 424.66584,230.73536 423.86545,234.46865 414.39771,236.46845 413.86433,239.66813 409.99697,236.73509 403.8631,235.80185 402.66265,233.66874 405.86266,231.13566 404.39584,224.73631 407.06279,221.93696 407.19614,217.00454 402.52898,211.00525 401.46255,204.73933 389.99435,207.00605 387.06071,211.4055 387.32706,222.20415 377.85934,219.93777 355.4564,218.33797 354.38926,226.20365 348.38853,227.80345 348.52187,233.93602 351.18886,239.53532 C351.18886,239.53532 356.12278,238.6021 355.72274,238.6021 355.32269,238.6021 361.99016,251.80045 361.99016,251.80045 L366.79074,253.53357 366.39069,258.5996 369.05768,259.13287 367.32414,268.73167 368.57429,275.93113 371.64132,279.19775 374.44166,279.73103 374.57501,286.46394 387.57658,287.19684 387.84328,290.06317 394.64409,291.66265 396.64434,285.79638 394.77744,284.99648 396.24429,279.99709 398.17785,279.13053 396.17761,276.99746 398.91128,274.99771 406.64554,277.86402 417.78022,267.79859 417.91357,262.53257 414.12144,259.02467 425.4228,249.8258 420.92226,244.72642 423.52258,243.02663 428.92323,242.82666 z" />
</Canvas>
Run Code Online (Sandbox Code Playgroud)
显示不正确
<Canvas>
<ItemsControl ItemsSource={Binding CanvasPaths}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Path Style="{StaticResource OverlayPath}"
Data="{Binding Data}"
Height="{Binding Height}"
Width="{Binding Width}"
Canvas.Left="{Binding CanvasLeft}"
Canvas.Top="{Binding CanvasTop}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
模型
public class CanvasPath
{
public CanvasPath(string data, double height, double width, double canvasTop, double canvasLeft)
{
Data = data;
Height = height;
Width = width;
CanvasTop = canvasTop;
CanvasLeft = canvasLeft;
}
public string Data { get; set; }
public double Height { get; set; }
public double Width { get; set; }
public double CanvasTop { get; set; }
public double CanvasLeft { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图模型
public class TestViewModel
{
private ObservableCollection<CanvasPath> test = new ObservableCollection<CanvasPath>()
{
new CanvasPath("M432.9,245.5 L428.26666,258.46667 439.86716,261.26627 443.46698,246.46662 443.06664,242.33348 437.06651,242.60046 428.26633,239.13402 429.9994,232.33489 424.66584,230.73536 423.86545,234.46865 414.39771,236.46845 413.86433,239.66813 409.99697,236.73509 403.8631,235.80185 402.66265,233.66874 405.86266,231.13566 404.39584,224.73631 407.06279,221.93696 407.19614,217.00454 402.52898,211.00525 401.46255,204.73933 389.99435,207.00605 387.06071,211.4055 387.32706,222.20415 377.85934,219.93777 355.4564,218.33797 354.38926,226.20365 348.38853,227.80345 348.52187,233.93602 351.18886,239.53532 C351.18886,239.53532 356.12278,238.6021 355.72274,238.6021 355.32269,238.6021 361.99016,251.80045 361.99016,251.80045 L366.79074,253.53357 366.39069,258.5996 369.05768,259.13287 367.32414,268.73167 368.57429,275.93113 371.64132,279.19775 374.44166,279.73103 374.57501,286.46394 387.57658,287.19684 387.84328,290.06317 394.64409,291.66265 396.64434,285.79638 394.77744,284.99648 396.24429,279.99709 398.17785,279.13053 396.17761,276.99746 398.91128,274.99771 406.64554,277.86402 417.78022,267.79859 417.91357,262.53257 414.12144,259.02467 425.4228,249.8258 420.92226,244.72642 423.52258,243.02663 428.92323,242.82666 z",
87.934, 96.067, 204.525, 348.456)
};
public ObservableCollection<CanvasPath> CanvasPaths
{
get
{
return test;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Canvas.Left和Canvas.Top绑定在你ItemTemplate没有任何效果,因为在DataTemplate中的路径控制不具备画布父.
为了使它工作,你必须设置ItemsPanel和这样的ItemContainerStyle属性:
<ItemsControl ItemsSource="{Binding CanvasPaths}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding CanvasLeft}"/>
<Setter Property="Canvas.Top" Value="{Binding CanvasTop}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Path Style="{StaticResource OverlayPath}"
Data="{Binding Data}"
Height="{Binding Height}"
Width="{Binding Width}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
217 次 |
| 最近记录: |