charri / Font-Awesome-WPF

FontAwesome controls for WPF+UWP
MIT License
533 stars 145 forks source link

TextBlock code-behind #24

Closed ImaginaryDevelopment closed 8 years ago

ImaginaryDevelopment commented 8 years ago

There's an example of setting an icon, but none of TextBlock purely in code-behind, how would I do that? so far in my attempts the binding error is FontAwesomeIcon property not found on 'object'.

charri commented 8 years ago

could you paste some code - would help me understand the issue.

ImaginaryDevelopment commented 8 years ago
            let createFAButton ()= 
                let b = Button()
                let t = TextBlock()
                let binding = new Binding()
                // binding.Path <- PropertyPath( "FontAwesomeIcon")
                // binding.Source <- ???
                binding.Source <- "Flag"
                binding.Converter <- CssClassNameConverter(Mode=CssClassConverterMode.FromStringToIcon)
                BindingOperations.SetBinding(t, TextBox.TextProperty, binding) |> ignore
                b.Content <- t
                // b.Content <- FontAwesome.WPF.ImageAwesome.CreateImageSource( FontAwesome.WPF.FontAwesomeIcon.FastBackward, Media.Brushes.Black)
                // let fa = FontAwesome.WPF.FontAwesome()
                // b.Content <- fa

                // b.FontFamily <- "pack://application:,,,/FontAwesome.WPF;component/#FontAwesome"
                b

lots of sample other attempts in comments

translation:

    Button CreateFAButton()
    {
        var b = new Button();
        var t = new TextBlock();
        var binding = new Binding();
        binding.Source = "Flag";
        binding.Converter = new CssClassNameConverter { Mode = CssClassConverterMode.FromStringToIcon);
        BindingOperations.SetBinding(t, TextBox.TextProperty, binding);
        b.Content = t;
        return b;        
    }
ImaginaryDevelopment commented 8 years ago

also tried

            let createFAButton ()= 
                let b = Button()
                FontAwesome.WPF.Awesome.SetContent(b, FontAwesome.WPF.FontAwesomeIcon.FastBackward)
                b

translated to C#:

Button CreateFAButton()
{
    var b = new Button();
    FontAwesome.WPF.Awesome.SetContent(b, FontAwesome.WPF.FontAwesomeIcon.FastBackward);
    return b;
}
charri commented 8 years ago

not tested but you should be able to do this:

let t = FontAwesome()

for more see: https://github.com/charri/Font-Awesome-WPF/blob/master/src/FontAwesome.WPF/FontAwesome.cs

ImaginaryDevelopment commented 8 years ago

tried making what I think your recommended adjustment was, did not work

            let createFAButton ()= 
                let b = Button()
                let t = FontAwesome.WPF.FontAwesome()
                let binding = Binding()
                binding.Source <- "Flag"
                binding.Converter <- CssClassNameConverter()
                BindingOperations.SetBinding(t, TextBox.TextProperty, binding) |> ignore

                b.Content <- t

                b
charri commented 8 years ago

Would you mind taking 5 min to look at the FontAwesome.cs code?

you could just assign the actual icon instead of the binding, as your not binding to anything that would cause a INotifyPropertyChanged event:

t.Icon = FontAwesomeIcon.Flag;

If you need to use bindings then you need to use the FontAwesome.IconProperty (TextProperty will not work)

cheers

ImaginaryDevelopment commented 8 years ago

that worked, I did look at the code you linked, twice. I have/had no idea how to interpret it. this code works, but I bet I don't need to be doing all that work. like I didn't actually need a binding.

working code:

            let createFAButton ()= 
                let b = Button()
                let t = FontAwesome.WPF.FontAwesome()
                t.Icon <- FontAwesome.WPF.FontAwesomeIcon.Flag
                let binding = Binding()
                binding.Source <- "Flag"
                binding.Converter <- CssClassNameConverter()
                BindingOperations.SetBinding(t, TextBox.TextProperty, binding) |> ignore
                b.Content <- t

                b

how would I do it without using a binding?

ImaginaryDevelopment commented 8 years ago

nevermind, didn't need the binding stuff at all

            let createFAButton ()= 
                let b = Button()
                let t = FontAwesome.WPF.FontAwesome()
                t.Icon <- FontAwesome.WPF.FontAwesomeIcon.Flag
                b.Content <- t

                b

thanks for the help and prompt replies.