Click here to Skip to main content
15,124,055 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: load usercontrol in background worker Pin
Gerry Schmitz12-Sep-20 5:47
mveGerry Schmitz12-Sep-20 5:47 
GeneralRe: load usercontrol in background worker Pin
Eddy Vluggen12-Sep-20 9:44
professionalEddy Vluggen12-Sep-20 9:44 
QuestionCan we add animation to WPF just like we have SHAPE animation in powerpoint? Pin
User-862169510-Jul-20 0:57
MemberUser-862169510-Jul-20 0:57 
QuestionVertical text in WPF grid Pin
Member 79891226-Jul-20 9:54
MemberMember 79891226-Jul-20 9:54 
AnswerRe: Vertical text in WPF grid Pin
Gerry Schmitz6-Jul-20 13:10
mveGerry Schmitz6-Jul-20 13:10 
AnswerRe: Vertical text in WPF grid Pin
Richard Deeming7-Jul-20 0:43
mveRichard Deeming7-Jul-20 0:43 
GeneralRe: Vertical text in WPF grid Pin
Member 79891227-Jul-20 6:03
MemberMember 79891227-Jul-20 6:03 
QuestionBehavior/DP Question Pin
Kevin Marois22-Jun-20 9:27
professionalKevin Marois22-Jun-20 9:27 
I have a behavior called SecurityBehavior that I'm using to control access to UI elements. I use it like this:
<TextBox Grid.Row="2" 
            Grid.Column="1"
            Text="{Binding EmployeeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Height="32"
            Width="250"
            local:SecurityBehavior.SecurityTags="can_add_employees|can_edit_employees" 
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="5"/>

I set two tags that allows the field to be enabled if the user can Add or Edit the employee name.

Here's my Behavior class:
public static class SecurityBehavior
{
    #region Security Tags
    public static string GetSecurityTags(DependencyObject obj)
    {
        return (string)obj.GetValue(SecurityTagsProperty);
    }

    public static void SetSecurityTags(DependencyObject obj, string value)
    {
        obj.SetValue(SecurityTagsProperty, value);
    }

    public static readonly DependencyProperty SecurityTagsProperty =
                                DependencyProperty.RegisterAttached("SecurityTags",
                                typeof(string), typeof(SecurityBehavior),
                                new PropertyMetadata("", OnSecurityTagsChanged));

    private static void OnSecurityTagsChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;

        bool canEnable = true;

        var tags = e.NewValue.ToString().Split('|').ToList();

        foreach (var tag in tags)
        {
            if (!string.IsNullOrEmpty(tag))
            {
                canEnable = canEnable & SecurityService.HasRights(tag);
            }
        }

        element.IsEnabled = canEnable;
    }
    #endregion
}
Pretty straightforward. But now I ned one more thing. The UI's are always disabled until the "Edit" button on the toolbar is clicked. So, I added another DP like this:
<h1>region Allow Enable</h1>

public static bool GetAllowEnable(DependencyObject obj)
{
    return (bool)obj.GetValue(AllowEnableProperty);
}

public static void SetAllowEnable(DependencyObject obj, bool value)
{
    obj.SetValue(AllowEnableProperty, value);
}

public static readonly DependencyProperty AllowEnableProperty =
                            DependencyProperty.RegisterAttached("AllowEnable",
                            typeof(bool), 
                            typeof(SecurityBehavior),
                            new PropertyMetadata(false, OnAllowEnableChanged));

private static void OnAllowEnableChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    FrameworkElement element = (FrameworkElement)sender;
}
<h1>endregion

and use it like this:
<TextBox Grid.Row="2" 
            Grid.Column="1"
            Text="{Binding EmployeeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Height="32"
            Width="250"
            local:SecurityBehavior.AllowEnable="{Binding AreFieldsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            local:SecurityBehavior.SecurityTags="can_add_employees|can_edit_employees" 
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="5"/>
The value of the VM's AreFieldsEnabled property is now passed to the DP AllowEnable. So far so good.

So I now tried to modify the OnSecurityTagsChanged method:
private static void OnSecurityTagsChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    FrameworkElement element = (FrameworkElement)sender;

    // ************************ AllowEnable is a DP in this class. This won't compile. How do I check the AllowEnable DP?
    var allowEnable = AllowEnable; 

    bool canEnable = true;

    var tags = e.NewValue.ToString().Split('|').ToList();

    foreach (var tag in tags)
    {
        if (!string.IsNullOrEmpty(tag))
        {
            canEnable = canEnable & SecurityService.HasRights(tag);
        }
    }

    element.IsEnabled = canEnable;
}
I get a compilation error on the line with the commnent. How do I read the value of the other DP??

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Behavior/DP Question Pin
Richard Deeming23-Jun-20 5:44
mveRichard Deeming23-Jun-20 5:44 
GeneralRe: Behavior/DP Question Pin
Kevin Marois1-Jul-20 9:30
professionalKevin Marois1-Jul-20 9:30 
GeneralRe: Behavior/DP Question Pin
Richard Deeming2-Jul-20 0:32
mveRichard Deeming2-Jul-20 0:32 
GeneralRe: Behavior/DP Question Pin
Kevin Marois2-Jul-20 9:49
professionalKevin Marois2-Jul-20 9:49 
Question2 xaml windows question Pin
Michele Smith26-May-20 8:10
MemberMichele Smith26-May-20 8:10 
AnswerRe: 2 xaml windows question Pin
#realJSOP26-May-20 8:43
mva#realJSOP26-May-20 8:43 
QuestionCreate DataGrid From List<T> With Properties As Columns Pin
Kevin Marois18-May-20 8:14
professionalKevin Marois18-May-20 8:14 
AnswerRe: Create DataGrid From List<T> With Properties As Columns Pin
Richard Deeming18-May-20 9:47
mveRichard Deeming18-May-20 9:47 
GeneralRe: Create DataGrid From List<T> With Properties As Columns Pin
Kevin Marois18-May-20 9:52
professionalKevin Marois18-May-20 9:52 
QuestionWPF ListBox Pin
michaelbarb7-May-20 11:03
Membermichaelbarb7-May-20 11:03 
AnswerRe: WPF ListBox Pin
Richard Deeming10-May-20 22:52
mveRichard Deeming10-May-20 22:52 
GeneralRe: WPF ListBox Pin
michaelbarb11-May-20 20:11
Membermichaelbarb11-May-20 20:11 
GeneralRe: WPF ListBox Pin
Mycroft Holmes11-May-20 22:33
professionalMycroft Holmes11-May-20 22:33 
AnswerRe: WPF ListBox Pin
Gerry Schmitz11-May-20 13:42
mveGerry Schmitz11-May-20 13:42 
QuestionDesign Question Pin
Kevin Marois18-Apr-20 16:26
professionalKevin Marois18-Apr-20 16:26 
AnswerRe: Design Question Pin
Mycroft Holmes19-Apr-20 13:20
professionalMycroft Holmes19-Apr-20 13:20 
GeneralRe: Design Question Pin
Kevin Marois19-Apr-20 15:09
professionalKevin Marois19-Apr-20 15:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.