2012-09-25 20 views
0

我有一個使用ShowDialog顯示的窗口。我試圖從用戶那裏獲得的價值之一是GB或TB的大小。所以,我對於這兩個控件,從WPF Extended Toolkit的IntegerUpDown和組合框:從兩個控件上的數據綁定對話框中的一個屬性

<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> 

我找不出如何將此綁定分配給對話框上的Capacity屬性。我希望WPF爲我自動設置Capacity屬性。有任何想法嗎?

+0

我真的不明白什麼是你想在這裏做。你想在控件中顯示'ComboBox'中選擇的GB或TB容量嗎? – Damascus

+0

@Damascus在UI中,我想通過使用IntegerUpDown和ComboBox控件來詢問容量。理想情況下,在關閉對話框之後,我不需要添加比上面顯示的更多的代碼來獲取容量(以字節爲單位)。我不想在UI中顯示容量的值。 –

回答

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); 
     } 
    } 
} 
相關問題