gpt4 book ai didi

wpf - 带有转换器的模板绑定(bind) - 有什么问题?

转载 作者:行者123 更新时间:2023-12-04 10:58:39 28 4
gpt4 key购买 nike

我正在创建一个游戏 table 。我想将字段大小(一个字段是正方形)指定为附加属性,并使用 ViewPort 的这个数据集值绘制 2x2 矩阵(并且平铺模式将完成游戏 table 的其余部分)。

我很茫然什么是错的,因为绑定(bind)不起作用。

在 XAML 中测试我想要的行为:

<DrawingBrush Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="None">

游戏 table 基于此 DrawingPaint 样本: http://msdn.microsoft.com/en-us/library/aa970904.aspx (图片在这里)

XAML:
<Window x:Class="Sokoban.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Sokoban"
Title="Window1" Height="559" Width="419">
<Window.Resources>
<local:FieldSizeToRectConverter x:Key="fieldSizeConverter" />
<Style x:Key="GameDesk" TargetType="{x:Type Rectangle}">
<Setter Property="local:GameDeskProperties.FieldSize" Value="50" />
<Setter Property="Fill">
<Setter.Value>
<!--<DrawingBrush Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="None">-->
<DrawingBrush Viewport="{TemplateBinding local:GameDeskProperties.FieldSize, Converter={StaticResource fieldSizeConverter}}" ViewportUnits="Absolute" TileMode="None">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="CornflowerBlue">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,100,100" />
</GeometryDrawing.Geometry>
</GeometryDrawing>

<GeometryDrawing Brush="Azure">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50" />
<RectangleGeometry Rect="50,50,50,50" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>

<StackPanel>
<Rectangle Style="{StaticResource GameDesk}" Width="300" Height="150" />
</StackPanel>
</Window>

转换器和属性定义:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Diagnostics;
using System.Windows.Data;

namespace Sokoban
{
public class GameDeskProperties : Panel
{

public static readonly DependencyProperty FieldSizeProperty;

static GameDeskProperties()
{
PropertyChangedCallback fieldSizeChanged =
new PropertyChangedCallback(OnFieldSizeChanged);
PropertyMetadata fieldSizeMetadata =
new PropertyMetadata(50, fieldSizeChanged);

FieldSizeProperty = DependencyProperty.RegisterAttached("FieldSize",
typeof(int), typeof(GameDeskProperties), fieldSizeMetadata);
}

public static int GetFieldSize(DependencyObject target)
{
return (int)target.GetValue(FieldSizeProperty);
}

public static void SetFieldSize(DependencyObject target, int value)
{
target.SetValue(FieldSizeProperty, value);
}


static void OnFieldSizeChanged(DependencyObject target,
DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("FieldSize just changed: " + e.NewValue);
}
}

[ValueConversion(/* sourceType */ typeof(int), /* targetType */ typeof(Rect))]
public class FieldSizeToRectConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Debug.Assert(targetType == typeof(int));

int fieldSize = int.Parse(value.ToString());
return new Rect(0, 0, 2 * fieldSize, 2 * fieldSize);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// should not be called in our example
throw new NotImplementedException();
}
}
}

最佳答案

TemplateBindings仅适用于在被模板化的控件上定义的依赖属性(在 ControlTemplate 中)。您只需将其切换为 BindingRelativeSourceAncestorType (另外,附加属性需要在绑定(bind)中使用括号):

...
<DrawingBrush Viewport="{Binding Path=(local:GameDeskProperties.FieldSize), Converter={StaticResource fieldSizeConverter}, RelativeSource={RelativeSource AncestorType={x:Type Rectangle}}}"
...

编辑 更新了 RelativeSource绑定(bind),因为它没有在 ControlTemplate 中定义.

关于wpf - 带有转换器的模板绑定(bind) - 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2586170/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com