Wpf 从两个控件在对话框上数据绑定一个属性

Wpf 从两个控件在对话框上数据绑定一个属性,wpf,data-binding,multibinding,Wpf,Data Binding,Multibinding,我有一个使用ShowDialog显示的窗口。我试图从用户那里得到的一个值是大小(GB或TB)。我有两个控件,一个来自的IntegerUpDown和一个ComboBox: <xctk:IntegerUpDown Name="SizeN" Minimum="1" Maximum="1023" Increment="1" Value="100"/> <ComboBox Name="SizeS" SelectedIndex="0"> <ComboBoxItem>

我有一个使用ShowDialog显示的窗口。我试图从用户那里得到的一个值是大小(GB或TB)。我有两个控件,一个来自的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>
我已经创建了一个IMultiValueConverter,PowerConverter,它接受一个int和一个字符串并返回ulong。我认为正确的多重绑定是:

<Window.Resources>
    <local:PowerConverter x:Key="CapacityConverter" />
</Window.Resources>

<MultiBinding Converter="{StaticResource CapacityConverter}">
    <Binding ElementName="SizeN" Path="Value" />
    <Binding ElementName="SizeS" Path="SelectedValue" />
</MultiBinding>


我不知道如何将此绑定分配给对话框上的Capacity属性。我希望WPF自动为我设置容量属性。有什么想法吗?

我必须将容量转换为CustomWindow上的DependencyProperty,在组合框上设置SelectedValuePath属性,并将绑定分配给样式中的容量

XAML:


我真的不明白你在这里想干什么。是否要在您的控件中以GB或TB显示在组合框中选择的
容量
。理想情况下,在对话框关闭后,我不需要添加比上面显示的更多的代码来获得容量(以字节为单位)。我不想在UI中显示容量值。
<Window.Resources>
    <local:PowerConverter x:Key="CapacityConverter" />
</Window.Resources>

<MultiBinding Converter="{StaticResource CapacityConverter}">
    <Binding ElementName="SizeN" Path="Value" />
    <Binding ElementName="SizeS" Path="SelectedValue" />
</MultiBinding>
<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);
        }
    }
}