【问题标题】:Databinding one property on a Dialog from two contols来自两个控件的 Dialog 上的数据绑定一个属性
【发布时间】:2012-09-25 13:16:06
【问题描述】:

我有一个使用 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>

我将对话框的 DataContext 设置为自身。我已经定义了容量属性:

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>

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

【问题讨论】:

  • 我不太明白你想在这里做什么。你想在你的控件中显示Capacity,在ComboBox中选择的GB或TB吗?
  • @Damascus 在 UI 中,我想通过使用 IntegerUpDown 和 ComboBox 控件来询问容量。理想情况下,我不需要添加比上面显示的更多的代码来获得对话框关闭后的容量(以字节为单位)。我不想在 UI 中显示容量的值。

标签: wpf data-binding multibinding


【解决方案1】:

我必须在 CustomWindow 上将 Capacity 转换为 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2010-12-07
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    相关资源
    最近更新 更多