2013-08-20 17 views
17

Bir Boolean değerini Brush'a dönüştürmek için kullanılması amaçlanan IValueConverter'u uygulayan bir sınıfa sahibim. Ben bir Border Kontrolünün BorderBrush özelliğine bağlama içinde IValueConverter kullanmaya çalışıyorum:BorderBrush özelliğinde bir bağlayıcıda bir IValueConverter kullanabilir misiniz?

public static readonly DependencyProperty DomainsProperty = 
    DependencyProperty.Register("Domains", typeof(ObservableCollection<DomainViewModel>), typeof(MainWindow), new PropertyMetadata(null)); 

public ObservableCollection<DomainViewModel> Domains { 
    get { return (ObservableCollection<DomainViewModel>) GetValue(DomainsProperty); } 
    set { SetValue(DomainsProperty, value); } 
} 

Ben derlemek: Burada

<Window x:Class="DomPicker.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:cc="clr-namespace:CustomControls;assembly=CustomControls" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib" 
     DataContext="{Binding Path=Domains, RelativeSource={RelativeSource Self}}" 
     Height="350" 
     Title="My Title" 
     Width="525"> 

    <cc:CarSystemWindow.Resources> 
     <cc:BooleanToBrushConverter x:Key="BrushConverter" True="Black" False="Transparent" /> 

     <DataTemplate x:Name="DomainTemplate" DataType="DomainViewModel"> 
      <Border BorderBrush="{Binding Converter=BrushConverter, Path=IsSelected}"> 
       . . . 
      </Border> 
     </DataTemplate> 
    </cc:CarSystemWindow.Resources> 

    <Grid> 
     <ListBox . . . Name="DomainListBox" /> 
    </Grid> 
<Window> 

kodda Domains özelliği için koddur İşte

The TypeConverter for "IValueConverter" does not support converting from a string. 

01 kodudur: hayır vasıta bitmiş gereğidir kod, ben BorderBrush bir derleyici hatası bağlayıcı olsun:

public class BooleanConverter<T> : IValueConverter { 

    public T False { get; set; } 
    public T True { get; set; } 

    public BooleanConverter(T trueValue, T falseValue) { 
     // Set the values of True and false to the values we were passed. 
     True = trueValue; 
     False = falseValue; 
    } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { 
     return value is bool && ((bool) value) ? True : False; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { 
     return value is T && EqualityComparer<T>.Default.Equals((T) value, True); 
    } 
} 

[ValueConversion(typeof(bool), typeof(Brush))] 
public class BooleanToBrushConverter : BooleanConverter<Brush> { 

    /// <summary> 
    /// Set us up so we convert true to a Black <see cref="SolidColorBrush"/> and 
    /// false to a Red SolidColorBrush. 
    /// </summary> 
    public BooleanToBrushConverter() : 
     base(new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.Red)) { } 
} 

kimse yanlış ne yapıyorum söyleyebilir misiniz?

cevap

55

Sen dönüştürücü

Örnek erişmek için StaticResource kullanmak gerekir:

<Border BorderBrush="{Binding IsSelected, Converter={StaticResource BrushConverter}}"> 
+1

Bunu biliyordum !!! Teşekkürler! –

+0

Oh vay. Şimdi aptalım. Teşekkürler @ sa_ddam213, sen benim günümü yaptın! :-) – BrainSlugs83

+0

Tam olarak iki yorumu ve aynı zamanda da aynı şeyi hissediyorum. –

İlgili konular