Repeatable pattern in WPF
up vote
0
down vote
favorite
I'm trying to achieve quite a specific thing in WPF. I need to create a verifiable control, which will be binded to object of generic class like this
public interface IVerifiable
{
bool Verified { get; set; }
}
public class VerifiableProperty<T> : INotifyPropertyChanged, IVerifiable
{
private T _value;
private bool _verified;
public T Value
{
get => _value;
set
{
if (Equals(value, _value)) return;
_value = value;
OnPropertyChanged();
}
}
public bool Verified
{
get => _verified;
set
{
if (value == _verified) return;
_verified = value;
OnPropertyChanged();
}
}
}
I bind controls this way
<TextBox Text="{Binding Path=Number.Value}" att:VerifiableControl.Verified="{Binding Path=Number.Verified}"
BorderBrush="{Binding Path=(att:VerifiableControl.Verified), Mode=OneWay,
Converter={StaticResource BackColorVerifiedConverter}}">
<inter:Interaction.Triggers>
<inter:EventTrigger EventName="LostFocus">
<inter:InvokeCommandAction Command="{Binding Path=DataContext.VerifyCurrentFieldCommand, ElementName=Root}"
CommandParameter="{Binding Path=Number}"/>
</inter:EventTrigger>
</inter:Interaction.Triggers>
</TextBox>
Generic value of Verifiable property object binds to text (or SelectedDate in case of DatePicker, etc.), border color binds to flag "Verified" via converter and also I bind interaction trigger for "FocusLost" event to ViewModel command that just set "Verified" flag of corresponding property object to true.
I really don't like the idea I have to copypaste this block many times with little changes. I know, I cannot put intercation trigger in style and I also couldn't find another way to make some kind of short pattern for that.
So, is there a way to create some kind of short pattern for this xaml code where I could customize name of property to bind? Or maybe you could suggest another approach for what I want ot achieve?
c# wpf xaml data-binding
add a comment |
up vote
0
down vote
favorite
I'm trying to achieve quite a specific thing in WPF. I need to create a verifiable control, which will be binded to object of generic class like this
public interface IVerifiable
{
bool Verified { get; set; }
}
public class VerifiableProperty<T> : INotifyPropertyChanged, IVerifiable
{
private T _value;
private bool _verified;
public T Value
{
get => _value;
set
{
if (Equals(value, _value)) return;
_value = value;
OnPropertyChanged();
}
}
public bool Verified
{
get => _verified;
set
{
if (value == _verified) return;
_verified = value;
OnPropertyChanged();
}
}
}
I bind controls this way
<TextBox Text="{Binding Path=Number.Value}" att:VerifiableControl.Verified="{Binding Path=Number.Verified}"
BorderBrush="{Binding Path=(att:VerifiableControl.Verified), Mode=OneWay,
Converter={StaticResource BackColorVerifiedConverter}}">
<inter:Interaction.Triggers>
<inter:EventTrigger EventName="LostFocus">
<inter:InvokeCommandAction Command="{Binding Path=DataContext.VerifyCurrentFieldCommand, ElementName=Root}"
CommandParameter="{Binding Path=Number}"/>
</inter:EventTrigger>
</inter:Interaction.Triggers>
</TextBox>
Generic value of Verifiable property object binds to text (or SelectedDate in case of DatePicker, etc.), border color binds to flag "Verified" via converter and also I bind interaction trigger for "FocusLost" event to ViewModel command that just set "Verified" flag of corresponding property object to true.
I really don't like the idea I have to copypaste this block many times with little changes. I know, I cannot put intercation trigger in style and I also couldn't find another way to make some kind of short pattern for that.
So, is there a way to create some kind of short pattern for this xaml code where I could customize name of property to bind? Or maybe you could suggest another approach for what I want ot achieve?
c# wpf xaml data-binding
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to achieve quite a specific thing in WPF. I need to create a verifiable control, which will be binded to object of generic class like this
public interface IVerifiable
{
bool Verified { get; set; }
}
public class VerifiableProperty<T> : INotifyPropertyChanged, IVerifiable
{
private T _value;
private bool _verified;
public T Value
{
get => _value;
set
{
if (Equals(value, _value)) return;
_value = value;
OnPropertyChanged();
}
}
public bool Verified
{
get => _verified;
set
{
if (value == _verified) return;
_verified = value;
OnPropertyChanged();
}
}
}
I bind controls this way
<TextBox Text="{Binding Path=Number.Value}" att:VerifiableControl.Verified="{Binding Path=Number.Verified}"
BorderBrush="{Binding Path=(att:VerifiableControl.Verified), Mode=OneWay,
Converter={StaticResource BackColorVerifiedConverter}}">
<inter:Interaction.Triggers>
<inter:EventTrigger EventName="LostFocus">
<inter:InvokeCommandAction Command="{Binding Path=DataContext.VerifyCurrentFieldCommand, ElementName=Root}"
CommandParameter="{Binding Path=Number}"/>
</inter:EventTrigger>
</inter:Interaction.Triggers>
</TextBox>
Generic value of Verifiable property object binds to text (or SelectedDate in case of DatePicker, etc.), border color binds to flag "Verified" via converter and also I bind interaction trigger for "FocusLost" event to ViewModel command that just set "Verified" flag of corresponding property object to true.
I really don't like the idea I have to copypaste this block many times with little changes. I know, I cannot put intercation trigger in style and I also couldn't find another way to make some kind of short pattern for that.
So, is there a way to create some kind of short pattern for this xaml code where I could customize name of property to bind? Or maybe you could suggest another approach for what I want ot achieve?
c# wpf xaml data-binding
I'm trying to achieve quite a specific thing in WPF. I need to create a verifiable control, which will be binded to object of generic class like this
public interface IVerifiable
{
bool Verified { get; set; }
}
public class VerifiableProperty<T> : INotifyPropertyChanged, IVerifiable
{
private T _value;
private bool _verified;
public T Value
{
get => _value;
set
{
if (Equals(value, _value)) return;
_value = value;
OnPropertyChanged();
}
}
public bool Verified
{
get => _verified;
set
{
if (value == _verified) return;
_verified = value;
OnPropertyChanged();
}
}
}
I bind controls this way
<TextBox Text="{Binding Path=Number.Value}" att:VerifiableControl.Verified="{Binding Path=Number.Verified}"
BorderBrush="{Binding Path=(att:VerifiableControl.Verified), Mode=OneWay,
Converter={StaticResource BackColorVerifiedConverter}}">
<inter:Interaction.Triggers>
<inter:EventTrigger EventName="LostFocus">
<inter:InvokeCommandAction Command="{Binding Path=DataContext.VerifyCurrentFieldCommand, ElementName=Root}"
CommandParameter="{Binding Path=Number}"/>
</inter:EventTrigger>
</inter:Interaction.Triggers>
</TextBox>
Generic value of Verifiable property object binds to text (or SelectedDate in case of DatePicker, etc.), border color binds to flag "Verified" via converter and also I bind interaction trigger for "FocusLost" event to ViewModel command that just set "Verified" flag of corresponding property object to true.
I really don't like the idea I have to copypaste this block many times with little changes. I know, I cannot put intercation trigger in style and I also couldn't find another way to make some kind of short pattern for that.
So, is there a way to create some kind of short pattern for this xaml code where I could customize name of property to bind? Or maybe you could suggest another approach for what I want ot achieve?
c# wpf xaml data-binding
c# wpf xaml data-binding
asked Nov 12 at 3:24
Andrey1661
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Use some attached properties and attach in there to the LostFocus event.
public static class VerifiableBehaviour
{
public static bool GetVerified(UIElement obj) => (bool)obj.GetValue(VerifiedProperty);
public static void SetVerified(DependencyObject obj, bool value) => obj.SetValue(VerifiedProperty, value);
public static readonly DependencyProperty VerifiedProperty = DependencyProperty.RegisterAttached(
"Verified", typeof(bool), typeof(VerifiableBehaviour),
new FrameworkPropertyMetadata(false)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
public static bool GetTracking(UIElement obj) => (bool)obj.GetValue(TrackingProperty);
public static void SetTracking(UIElement obj, bool value) => obj.SetValue(TrackingProperty, value);
public static readonly DependencyProperty TrackingProperty = DependencyProperty.RegisterAttached(
"Tracking", typeof(bool), typeof(VerifiableBehaviour),
new PropertyMetadata(false, Tracking_PropertyChanged));
private static void Tracking_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if ((bool)e.NewValue)
element.LostFocus += Element_LostFocus;
else
element.LostFocus -= Element_LostFocus;
}
private static void Element_LostFocus(object sender, RoutedEventArgs e)
{
UIElement element = sender as UIElement;
SetVerified(element, true);
}
}
and bind it to your controls
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="VerifiableText">
<StackPanel>
<TextBox b:VerifiableBehaviour.Tracking="True"
b:VerifiableBehaviour.Verified="{Binding Verified}"
Text="{Binding Value}"/>
<TextBlock>(Verified: <Run Text="{Binding Verified}"/>)</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<TextBlock Text="MyProperty1"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty1}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty2"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty2}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty3"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty3}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty4"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty4}" IsTabStop="False"/>
<Separator/>
</StackPanel>
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Use some attached properties and attach in there to the LostFocus event.
public static class VerifiableBehaviour
{
public static bool GetVerified(UIElement obj) => (bool)obj.GetValue(VerifiedProperty);
public static void SetVerified(DependencyObject obj, bool value) => obj.SetValue(VerifiedProperty, value);
public static readonly DependencyProperty VerifiedProperty = DependencyProperty.RegisterAttached(
"Verified", typeof(bool), typeof(VerifiableBehaviour),
new FrameworkPropertyMetadata(false)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
public static bool GetTracking(UIElement obj) => (bool)obj.GetValue(TrackingProperty);
public static void SetTracking(UIElement obj, bool value) => obj.SetValue(TrackingProperty, value);
public static readonly DependencyProperty TrackingProperty = DependencyProperty.RegisterAttached(
"Tracking", typeof(bool), typeof(VerifiableBehaviour),
new PropertyMetadata(false, Tracking_PropertyChanged));
private static void Tracking_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if ((bool)e.NewValue)
element.LostFocus += Element_LostFocus;
else
element.LostFocus -= Element_LostFocus;
}
private static void Element_LostFocus(object sender, RoutedEventArgs e)
{
UIElement element = sender as UIElement;
SetVerified(element, true);
}
}
and bind it to your controls
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="VerifiableText">
<StackPanel>
<TextBox b:VerifiableBehaviour.Tracking="True"
b:VerifiableBehaviour.Verified="{Binding Verified}"
Text="{Binding Value}"/>
<TextBlock>(Verified: <Run Text="{Binding Verified}"/>)</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<TextBlock Text="MyProperty1"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty1}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty2"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty2}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty3"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty3}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty4"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty4}" IsTabStop="False"/>
<Separator/>
</StackPanel>
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
add a comment |
up vote
1
down vote
accepted
Use some attached properties and attach in there to the LostFocus event.
public static class VerifiableBehaviour
{
public static bool GetVerified(UIElement obj) => (bool)obj.GetValue(VerifiedProperty);
public static void SetVerified(DependencyObject obj, bool value) => obj.SetValue(VerifiedProperty, value);
public static readonly DependencyProperty VerifiedProperty = DependencyProperty.RegisterAttached(
"Verified", typeof(bool), typeof(VerifiableBehaviour),
new FrameworkPropertyMetadata(false)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
public static bool GetTracking(UIElement obj) => (bool)obj.GetValue(TrackingProperty);
public static void SetTracking(UIElement obj, bool value) => obj.SetValue(TrackingProperty, value);
public static readonly DependencyProperty TrackingProperty = DependencyProperty.RegisterAttached(
"Tracking", typeof(bool), typeof(VerifiableBehaviour),
new PropertyMetadata(false, Tracking_PropertyChanged));
private static void Tracking_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if ((bool)e.NewValue)
element.LostFocus += Element_LostFocus;
else
element.LostFocus -= Element_LostFocus;
}
private static void Element_LostFocus(object sender, RoutedEventArgs e)
{
UIElement element = sender as UIElement;
SetVerified(element, true);
}
}
and bind it to your controls
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="VerifiableText">
<StackPanel>
<TextBox b:VerifiableBehaviour.Tracking="True"
b:VerifiableBehaviour.Verified="{Binding Verified}"
Text="{Binding Value}"/>
<TextBlock>(Verified: <Run Text="{Binding Verified}"/>)</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<TextBlock Text="MyProperty1"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty1}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty2"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty2}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty3"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty3}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty4"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty4}" IsTabStop="False"/>
<Separator/>
</StackPanel>
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use some attached properties and attach in there to the LostFocus event.
public static class VerifiableBehaviour
{
public static bool GetVerified(UIElement obj) => (bool)obj.GetValue(VerifiedProperty);
public static void SetVerified(DependencyObject obj, bool value) => obj.SetValue(VerifiedProperty, value);
public static readonly DependencyProperty VerifiedProperty = DependencyProperty.RegisterAttached(
"Verified", typeof(bool), typeof(VerifiableBehaviour),
new FrameworkPropertyMetadata(false)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
public static bool GetTracking(UIElement obj) => (bool)obj.GetValue(TrackingProperty);
public static void SetTracking(UIElement obj, bool value) => obj.SetValue(TrackingProperty, value);
public static readonly DependencyProperty TrackingProperty = DependencyProperty.RegisterAttached(
"Tracking", typeof(bool), typeof(VerifiableBehaviour),
new PropertyMetadata(false, Tracking_PropertyChanged));
private static void Tracking_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if ((bool)e.NewValue)
element.LostFocus += Element_LostFocus;
else
element.LostFocus -= Element_LostFocus;
}
private static void Element_LostFocus(object sender, RoutedEventArgs e)
{
UIElement element = sender as UIElement;
SetVerified(element, true);
}
}
and bind it to your controls
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="VerifiableText">
<StackPanel>
<TextBox b:VerifiableBehaviour.Tracking="True"
b:VerifiableBehaviour.Verified="{Binding Verified}"
Text="{Binding Value}"/>
<TextBlock>(Verified: <Run Text="{Binding Verified}"/>)</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<TextBlock Text="MyProperty1"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty1}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty2"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty2}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty3"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty3}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty4"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty4}" IsTabStop="False"/>
<Separator/>
</StackPanel>
Use some attached properties and attach in there to the LostFocus event.
public static class VerifiableBehaviour
{
public static bool GetVerified(UIElement obj) => (bool)obj.GetValue(VerifiedProperty);
public static void SetVerified(DependencyObject obj, bool value) => obj.SetValue(VerifiedProperty, value);
public static readonly DependencyProperty VerifiedProperty = DependencyProperty.RegisterAttached(
"Verified", typeof(bool), typeof(VerifiableBehaviour),
new FrameworkPropertyMetadata(false)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
});
public static bool GetTracking(UIElement obj) => (bool)obj.GetValue(TrackingProperty);
public static void SetTracking(UIElement obj, bool value) => obj.SetValue(TrackingProperty, value);
public static readonly DependencyProperty TrackingProperty = DependencyProperty.RegisterAttached(
"Tracking", typeof(bool), typeof(VerifiableBehaviour),
new PropertyMetadata(false, Tracking_PropertyChanged));
private static void Tracking_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if ((bool)e.NewValue)
element.LostFocus += Element_LostFocus;
else
element.LostFocus -= Element_LostFocus;
}
private static void Element_LostFocus(object sender, RoutedEventArgs e)
{
UIElement element = sender as UIElement;
SetVerified(element, true);
}
}
and bind it to your controls
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="VerifiableText">
<StackPanel>
<TextBox b:VerifiableBehaviour.Tracking="True"
b:VerifiableBehaviour.Verified="{Binding Verified}"
Text="{Binding Value}"/>
<TextBlock>(Verified: <Run Text="{Binding Verified}"/>)</TextBlock>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<TextBlock Text="MyProperty1"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty1}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty2"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty2}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty3"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty3}" IsTabStop="False"/>
<Separator/>
<TextBlock Text="MyProperty4"/>
<ContentControl ContentTemplate="{StaticResource VerifiableText}"
Content="{Binding MyProperty4}" IsTabStop="False"/>
<Separator/>
</StackPanel>
answered Nov 12 at 8:04
Sir Rufo
14.3k22757
14.3k22757
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
add a comment |
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
Got the idea, thank you. Didn't try data template for that. So, I guess, I need to provide a data template for each type of control I want to use
– Andrey1661
Nov 12 at 8:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255570%2frepeatable-pattern-in-wpf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown