我有一个使用ShowDialog显示的窗口。我试图从用户那里得到的一个值是GB或TB的大小。所以我有两个控件,一个来自WPF Extended Toolkit的IntegerUpDown和一个ComboBox:
<xctk:IntegerUpDown Name="SizeN" Minimum="1" Maximum="1023" Increment="1" Value="100"/>
<ComboBox Name="SizeS" SelectedIndex="0">
<ComboBoxItem>GB</ComboBoxItem>
<ComboBoxItem>TB</ComboBoxItem>
</ComboBox>
我正在将Dialog的DataContext设置为自身。我已经定义了Capacity属性:
public ulong Capacity { get; set; }
public CustomWindow()
{
InitializeComponent();
DataContext = this;
}
我已经创建了一个IMultiValueConverter,PowerConverter,它接受一个int和一个字符串并返回ulong。我认为正确的MultiBinding是:
<Window.Resources>
<local:PowerConverter x:Key="CapacityConverter" />
</Window.Resources>
<MultiBinding Converter="{StaticResource CapacityConverter}">
<Binding ElementName="SizeN" Path="Value" />
<Binding ElementName="SizeS" Path="SelectedValue" />
</MultiBinding>
我无法弄清楚如何将此绑定分配给Dialog上的Capacity属性。我希望WPF为我自动设置Capacity属性。有什么想法吗?
答案 0 :(得分:0)
我必须将Capacity转换为CustomWindow上的DependencyProperty,在ComboBox上设置SelectedValuePath属性,并将绑定分配给样式中的Capacity。
XAML:
<Window xmlns:="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=WPFToolkit.Extended"
xmlns:local="clr-namespace:MyProject"
x:Class="MyProject.CustomWindow" Title="CustomWindow"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<local:PowerConverter x:Key="CapacityConverter" />
</Window.Resources>
<Window.Style>
<Style TargetType="{x:Type local:CustomWindow}">
<Setter Property="Capacity">
<Setter.Value>
<MultiBinding Converter="{StaticResource CapacityConverter}"
Mode="TwoWay">
<Binding ElementName="SizeNumber" Path="Value"
Mode="TwoWay" />
<Binding ElementName="SizeSuffix" Path="SelectedValue"
Mode="TwoWay" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Style>
<StackPanel>
<xctk:IntegerUpDown Name="SizeNumber" Minimum="1" Maximum="1023" Increment="1"
Value="100"/>
<ComboBox Name="SizeSuffix" SelectedIndex="0" SelectedValuePath="Content">
<ComboBoxItem>GB</ComboBoxItem>
<ComboBoxItem>TB</ComboBoxItem>
</ComboBox>
</StackPanel>
</Window>
代码背后:
public partial class CustomWindow : Window
{
public CustomWindow()
{
InitializeComponent();
}
public static readonly DependencyProperty CapacityProperty =
DependencyProperty.Register("Capacity", typeof(ulong), typeof(CustomWindow));
public ulong Capacity
{
get
{
return (ulong)GetValue(CapacityProperty);
}
set
{
SetValue(CapacityProperty, value);
}
}
}