我有一组形状,我需要决定哪些可见,哪些隐藏 根据拖曳条件(这将代表骰子视觉)
(1)他们通过绑定从他们的数据上下文中获得的值。
(2)它们在我为每个形状保存的组中的位置值是否为Tag属性
我需要转换器将Tag属性作为每个Shape的参数
我的约束力:(不正当)
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Visibility" Value="{Binding Path=., Converter={StaticResource MyDiceInputToVisualConverter}}"></Setter>
</Style>
我的形状:
<Canvas DataContext="{Binding Path=DataContext.Dice1,RelativeSource={RelativeSource AncestorType=StackPanel}}">
<Ellipse Tag="1" Canvas.Left="5" Canvas.Top="5"></Ellipse>
<Ellipse Tag="2" Canvas.Left="5" Canvas.Top="20"></Ellipse>
<Ellipse Tag="3" Canvas.Left="5" Canvas.Top="35"></Ellipse>
<Ellipse Tag="4" Canvas.Left="20" Canvas.Top="20"></Ellipse>
<Ellipse Tag="5" Canvas.Left="35" Canvas.Top="5"></Ellipse>
<Ellipse Tag="6" Canvas.Left="35" Canvas.Top="20"></Ellipse>
<Ellipse Tag="7" Canvas.Left="35" Canvas.Top="35"></Ellipse>
</Canvas>
我的转换器不应该感兴趣所有它返回可见或隐藏根据 骰子值和骰子上点的位置。
如何向转换器发送标签属性或者在转换器中引用它?
提前谢谢。编辑:
我最终在每个椭圆的标记中明确设置了绑定:
<Canvas DataContext="{Binding Path=DataContext.Dice1,RelativeSource={RelativeSource AncestorType=StackPanel}}">
<Ellipse Tag="1" Canvas.Left="5" Canvas.Top="5" Visibility="{Binding Path=., Converter={StaticResource MyDiceInputToVisualConverter},ConverterParameter=1}"></Ellipse>
<Ellipse Tag="2" Canvas.Left="5" Canvas.Top="20" Visibility="{Binding Path=., Converter={StaticResource MyDiceInputToVisualConverter},ConverterParameter=2}"></Ellipse>
<Ellipse Tag="3" Canvas.Left="5" Canvas.Top="35" Visibility="{Binding Path=., Converter={StaticResource MyDiceInputToVisualConverter},ConverterParameter=3}"></Ellipse>
</Canvas>
如果任何人知道如何将其合并到椭圆的样式中以避免所有这些代码重复,请告诉我。谢谢 。
答案 0 :(得分:2)
我认为您需要多重绑定:
<Page.Resources>
<WPFSampleExplorer:MyDiceInputToVisualConverter x:Key="MyDiceInputToVisualConverter" />
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource MyDiceInputToVisualConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Tag"/>
<Binding />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Page.DataContext>
<Samples:DiceViewModel/>
</Page.DataContext>
<Canvas>
<Ellipse Tag="1" Canvas.Left="5" Canvas.Top="5" Width="20" Height="20" Fill="Yellow"></Ellipse>
<Ellipse Tag="2" Canvas.Left="5" Canvas.Top="20" Width="20" Height="20" Fill="Yellow"></Ellipse>
<Ellipse Tag="3" Canvas.Left="5" Canvas.Top="35" Width="20" Height="20" Fill="Yellow"></Ellipse>
<Ellipse Tag="4" Canvas.Left="20" Canvas.Top="20" Width="20" Height="20" Fill="Yellow"></Ellipse>
<Ellipse Tag="5" Canvas.Left="35" Canvas.Top="5" Width="20" Height="20" Fill="Yellow"></Ellipse>
<Ellipse Tag="6" Canvas.Left="35" Canvas.Top="20" Width="20" Height="20" Fill="Yellow"></Ellipse>
<Ellipse Tag="7" Canvas.Left="35" Canvas.Top="35" Width="20" Height="20" Fill="Yellow"></Ellipse>
</Canvas>
public class DiceViewModel
{
// properties here
}
public class MyDiceInputToVisualConverter : IMultiValueConverter
{
#region Implementation of IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
Debug.WriteLine(values[0]); // This outputs 1,2,3,..7
Debug.WriteLine(values[1]); // This is your DiceViewModel
return Visibility.Visible;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
答案 1 :(得分:0)
如果省略ConverterParameter,转换器将接收完整对象(Ellipse)。然后将您的Value转换为Ellipse并获取标记。