[英]Why does MultiBinding a TranslateTransform.X seem not to work in WPF?
I hope I am not being silly asking this, but I am trying to design a graph viewer in WPF being a total newb in the framework; 我希望我不要愚蠢地问这个问题,但是我试图在WPF中设计一个图表查看器,使它成为框架中的一个新手。 although I did come up with a couple of solutions to my problems, the one I'm most proud of... does not work where it should.
尽管我确实为我的问题提出了一些解决方案,但我最引以为傲的解决方案无法在应有的位置起作用。 What I'm trying to do is to place a node on the ItemsControl at a position I calculated with Dijkstra's alg + some maths.
我想做的是在ItemsControl上的一个节点上放置一个我用Dijkstra的算法+一些数学计算出的位置 。 The problem is that what I move around when using a normal X and Y Binding is the upper-left corner of the TextBlock with Border but what I'd like to manpulate is their center instead.
问题是,当我使用普通的X和Y绑定时,我到处走动的是带有边框的TextBlock的左上角,但是我想操纵的是它们的中心 。 Thus, my nodes end up sticking down and to the right from the point I specify, not centered on it.
因此,我的节点最终从我指定的点开始向下并向右伸出,而不是居中。
I finally settled for a Multibinding using the X/Y property of my NodeViewModel and the Grid's ActualWidth/Height. 我终于使用NodeViewModel的X / Y属性和网格的ActualWidth / Height解决了Multibinding问题。 What happens is that all my nodes get placed at (0,0) !
发生的事情是我所有的节点都放在(0,0) ! I even debugged the code and looked at the converter, but the return value seems OK.
我什至调试了代码并查看了转换器,但是返回值似乎还可以。 I even tried some random things like binding to other properties etc.
我什至尝试了一些随机的事情,例如绑定到其他属性等。
I am totally puzzled. 我完全不解。
So one question would be - does MultiBinding work that way? 因此,一个问题将是-MultiBinding是否以这种方式工作? Or am I making some stupid mistake?
还是我犯了一些愚蠢的错误? I'll post my Converter below the XAML code.
我将在XAML代码下面发布我的Converter。 The parts in XAML cut out are a tooltip and an embedded ItemsControl, but cutting these out changes nothing (except for improving the clarity of the code).
XAML中切出的部分是一个工具提示和一个嵌入式ItemsControl,但是切出这些内容不会改变任何东西(除了提高代码的清晰度之外)。 The casting in the converter is because casting directly to float does not work for me (but that's irrelevat - it works the way it is even if it's not very beautiful).
转换器中的转换是因为直接对float进行转换对我不起作用(但这无关紧要-即使它不是很漂亮,它的工作方式也是如此)。
And another question would be - can I do it in any simpler way? 另一个问题是-我能以任何更简单的方式做到吗? Like manipulate the center of the TextBlock directly?
喜欢直接操纵TextBlock的中心吗?
XAML: XAML:
<Grid>
<ItemsControl ItemsSource="{Binding Source={StaticResource Nodes}, Path=Nodes}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="vievModels:NodeViewModel">
<Grid>
<Grid x:Name="nodeGrid">
<Grid.RenderTransform>
<TranslateTransform>
<TranslateTransform.X>
<MultiBinding Converter="{StaticResource PositionConverter}">
<Binding Path="Position.X"/>
<Binding ElementName="nodeGrid" Path="ActualWidth"/>
</MultiBinding>
</TranslateTransform.X>
<TranslateTransform.Y>
<MultiBinding Converter="{StaticResource PositionConverter}">
<Binding Path="Position.Y"/>
<Binding ElementName="nodeGrid" Path="ActualHeight"/>
</MultiBinding>
</TranslateTransform.Y>
</TranslateTransform>
</Grid.RenderTransform>
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="7">
<Border.Background>
<SolidColorBrush Color="{Binding Color}" Opacity="0.5"/>
</Border.Background>
<TextBlock x:Name="Label" Margin="5,5,5,5" MaxWidth="160" TextAlignment="Center" TextWrapping="Wrap" FontFamily="Lucida Console" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Path=Label}"/>
</Border>
...
</Grid>
...
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Transparent"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
C#: C#:
public class PositionConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
float point = float.Parse(values[0].ToString());
float size = float.Parse(values[1].ToString());
return point - size / 2;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Cheers, Michael 干杯,迈克尔
Just as I thought - a silly mistake. 正如我所想-一个愚蠢的错误。 The converted property is of double precision and cannot have float assigned to it.
转换后的属性具有双精度,并且不能为其分配浮点数。 I had to learn how to turn on the debug output to see that.
我必须学习如何打开调试输出才能看到。
如果在Grid上设置RenderTransformOrigin =“。5,.5”,则它应与原始值绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.