我正在研究一個小型的大學項目,我需要用一些表格和函數讀取一個Lua文件,並從中做出一個形狀。完成了。爲什麼雙向數據綁定在WPF中不起作用?
問題是當我嘗試使它交互。這是我的XAML:
<Window x:Class="Gemi.WPF.VentanaPrincipal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Gemi.WPF"
Title="GEMI - Geometría Interactiva!" Height="350" Width="525">
<Window.Resources>
<local:DoblesAPunto x:Key="DoblesAPunto"/>
</Window.Resources>
<DockPanel LastChildFill="True" Background="White">
<Grid DockPanel.Dock="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="Figura Seleccionada: "/>
<ComboBox Name="cbFiguras" HorizontalAlignment="Stretch" Grid.Column="1"
ItemsSource="{Binding Path=Figuras}" DisplayMemberPath="Nombre" SelectionChanged="cbFiguras_FiguraSeleccionada" />
</Grid>
<ScrollViewer DockPanel.Dock="Bottom" Name="scrollPropiedades"
Height="Auto" VerticalScrollBarVisibility="Auto">
<DockPanel Name="dpPropiedades">
<StackPanel Orientation="Vertical" Name="spNombres" DockPanel.Dock="Left"/>
<StackPanel Orientation="Vertical" Name="spValores" DockPanel.Dock="Right" Margin="10, 0, 0, 0"/>
</DockPanel>
</ScrollViewer>
<DockPanel LastChildFill="True">
<Slider Name="controlZoom" DockPanel.Dock="Bottom" Value="1"
Maximum="50" Minimum="0.1" Orientation="Horizontal" ToolTip="Controla el zoom de la figura"/>
<ItemsControl x:Name="cnvFigura" ItemsSource="{Binding Puntos}">
<ItemsControl.LayoutTransform>
<ScaleTransform
CenterX="0" CenterY="0"
ScaleX="{Binding ElementName=controlZoom,Path=Value}"
ScaleY="{Binding ElementName=controlZoom,Path=Value}"/>
</ItemsControl.LayoutTransform>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Path Fill="Black" local:DragCanvas.CanBeDragged="True"
local:DragCanvas.Top="{Binding Y, Mode=TwoWay}"
local:DragCanvas.Left="{Binding X, Mode=TwoWay}">
<Path.Data>
<EllipseGeometry RadiusX="5" RadiusY="5">
<EllipseGeometry.Center>
<MultiBinding Converter="{StaticResource DoblesAPunto}">
<Binding Path="X" />
<Binding Path="Y" />
</MultiBinding>
</EllipseGeometry.Center>
</EllipseGeometry>
</Path.Data>
<Path.ToolTip>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="X = " Grid.Column="0" Grid.Row="0"/>
<TextBlock Text="{Binding X}" Grid.Column="1" Grid.Row="0"/>
<TextBlock Text="Y = " Grid.Column="0" Grid.Row="1"/>
<TextBlock Text="{Binding Y}" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Path.ToolTip>
</Path>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:DragCanvas IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DockPanel>
</DockPanel>
</Window>
問題在於綁定不會更新。即使拖動組成DataTemplate的省略號,也不會調用「Punto」對象的setter。有任何想法嗎?
此外,爲什麼我似乎需要綁定橢圓的中心?如果我只綁定DragCanvas的頂部/底部/左/右,所有的點都繪製在0,0,這會引發一個問題,因爲我不能將點進一步左移,也不能將頂點拖到最初的位置。
也許是缺少模式= TwoWay? – daryal