2012-12-19 105 views
0

所以我創建了一個帶有「ShowCode」屬性的userControl來學習如何使用屬性。我想要這個屬性隱藏網格中的第二行。爲什麼綁定不起作用?

查看:

<UserControl x:Class="Test.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid Margin="0,0,0,0" Name="outergrid" DataContext="{Binding}"> 
     <Grid.RowDefinitions> 
     <RowDefinition Height="3*" /> 
     <RowDefinition Height="1*" /> 
    </Grid.RowDefinitions> 

    <ContentControl Name="XAMLView" Grid.Row="0"/> 
    <GridSplitter ResizeDirection="Rows" 
       Grid.Row="0" 
       VerticalAlignment="Bottom" 
       HorizontalAlignment="Stretch" /> 
    <Border Width="11" Grid.Row="1" Background="Black" /> 
</Grid> 

代碼:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
     outergrid.RowDefinitions[1].SetBinding(RowDefinition.HeightProperty, new Binding() { Path = new PropertyPath("ShowCode"), Source = this, Converter = new BoolToHeightConverter(), ConverterParameter = "True" }); 
    } 

    public bool ShowCode 
    { 
     get { return (bool)GetValue(ShowCodeProperty); } 
     set { SetValue(ShowCodeProperty, value); } 
    } 

    public static readonly DependencyProperty ShowCodeProperty = 
     DependencyProperty.Register("ShowCode", 
     typeof(bool), 
     typeof(UserControl1), 
     new PropertyMetadata(true, new PropertyChangedCallback(OnShowCodeChanged))); 

    static void OnShowCodeChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
     UserControl1 source = (UserControl1)sender; 

     //source.outergrid.RowDefinitions[1].Height = 
    } 

    public class BoolToHeightConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, 
      object parameter, System.Globalization.CultureInfo culture) 
     { 
      if ((string)parameter == "False") return "0"; 
      return "1*"; 
     } 

     public object ConvertBack(object value, Type targetType, 
      object parameter, System.Globalization.CultureInfo culture) 
     { 
      if ((int)parameter == 0) return false; 
      return true; 
     } 
    } 
} 

問題:當我使用它是這樣的:

<xamlviewer:UserControl1 ShowCode="False"/> 

轉換(...)被調用2次和兩次「參數」呃「是」真「,否則如果ShowCode =」True「Convert()只被調用一次而」參數「又是」True「

爲什麼總是這樣?

回答

1

這裏至少有兩件事是錯誤的。

  1. 您的轉換器返回一個字符串,它應該返回GridLength

  2. 要轉換的值被傳遞給轉換器的value參數,而不是parameter,它是bool

所以,你應該這樣寫:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    if (value is bool && (bool)value) 
    { 
     return new GridLength(1, GridUnitType.Star); 
    } 

    return new GridLength(0); 
} 

需要在結合無轉換器參數:

outergrid.RowDefinitions[1].SetBinding(
    RowDefinition.HeightProperty, 
    new Binding() 
    { 
     Path = new PropertyPath("ShowCode"), 
     Source = this, 
     Converter = new BoolToHeightConverter() 
    }); 
1

參數始終是正確的,因爲你做它有一個值:

outergrid.RowDefinitions[1].SetBinding(
    RowDefinition.HeightProperty, 
    new Binding() 
    { 
     Path = new PropertyPath("ShowCode"), 
     Source = this, 
     Converter = new BoolToHeightConverter(), 
     ConverterParameter = "True" // <---- You are enforcing its value here. 
    }); 

你的轉換器是搞砸反正:你必須檢查valueparameter正確地轉換數據來回:

// NOTE: Your property is BOOL not STRING. 
public class BoolToHeightConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
     object parameter, System.Globalization.CultureInfo culture) 
    { 
     if(value is bool) 
      if((bool)value) return new GridLength(1, DataGridLengthUnitType.Star); 
      else return new GridLength(0); 

     throw new ArgumentException("Not a bool"); 
    } 

    public object ConvertBack(object value, Type targetType, 
     object parameter, System.Globalization.CultureInfo culture) 
    { 
     // You may not need this method at all! 
     if(value is GridLength) 
      return ((GridLength)value).Value == 0); 
     throw new ArgumentException("Not a GridLength"); 
    } 
} 

參數不再需要(除非其他地方有其他需求)。

相關問題