Created
April 23, 2012 18:41
-
-
Save timheuer/2472969 to your computer and use it in GitHub Desktop.
LengthConverters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Page.Resources> | |
<local:LengthConverter x:Key="lc" /> | |
<local:LengthBoolConverter x:Key="lb" /> | |
</Page.Resources> | |
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> | |
<Grid.Resources> | |
<local:MyViewModel x:Key="MyVM" BoundText="{Binding Path=Text, ElementName=inputbox, Mode=TwoWay}" /> | |
</Grid.Resources> | |
<StackPanel DataContext="{Binding Source={StaticResource MyVM}}"> | |
<TextBox x:Name="inputbox" FontSize="24.667" Text="{Binding BoundText, Mode=TwoWay}" /> | |
<Button Content="test" IsEnabled="{Binding ElementName=inputbox, Path=Text.Length, Converter={StaticResource lb}}" /> | |
<TextBlock x:Name="outputbox" FontSize="24.667" Text="{Binding ElementName=inputbox, Path=Text.Length, Converter={StaticResource lc}}" /> | |
<TextBlock x:Name="vmcount" FontSize="24.667" Text="{Binding BoundText}" /> | |
</StackPanel> | |
</Grid> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LengthConverter:IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, string language) | |
{ | |
return (140 - (int)value); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, string language) | |
{ | |
return value; | |
} | |
} | |
public class LengthBoolConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, string language) | |
{ | |
if ((int)value > 140) { return false; } | |
return true; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, string language) | |
{ | |
return value; | |
} | |
} | |
/// <summary> | |
/// An empty page that can be used on its own or navigated to within a Frame. | |
/// </summary> | |
public sealed partial class MainPage : Page | |
{ | |
public MainPage() | |
{ | |
this.InitializeComponent(); | |
} | |
} | |
public class MyViewModel : DependencyObject, INotifyPropertyChanged | |
{ | |
public string BoundText | |
{ | |
get { return (string)GetValue(BoundTextProperty); } | |
set { SetValue(BoundTextProperty, value); NotifyChanged("BoundText"); } | |
} | |
public static readonly DependencyProperty BoundTextProperty = | |
DependencyProperty.Register("BoundText", typeof(string), typeof(MyViewModel), null); | |
private int _boundLen; | |
public int BoundLen | |
{ | |
get { return _boundLen; } | |
set | |
{ | |
_boundLen = value; | |
NotifyChanged("BoundLen"); | |
} | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
public void NotifyChanged(string propertyName) | |
{ | |
if (null != PropertyChanged) | |
{ | |
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment