1
votes

I have a TextBlock and to show translated text I use

x:Uid="/FileResourceName/txtControldName" Name="txtControlName"

(in resource file I write: txtControldName.Text = "some text") it works fine. But I would like to hide or show it depend on codebehind object and for that I use

Visibility="{Binding Path = IsMyControlVisible}"

(in that case for text I have to assign some text directly in control like Text="some text"). If I use one of this two properties everything works fine but simultaneously these two properties do not work. Is there any way to do the same?

1

1 Answers

1
votes

If I use one of this two properties everything works fine but simultaneously these two properties do not work. Is there any way to do the same?

It's not a normal behavior. There is no conflict between binding to Visibility property and setting text in resource file. Have you set DataContext for your Binding?

Please see the following code sample, it worked well.

enter image description here

<Grid>
    <TextBlock x:Uid="txtControldName" Visibility="{Binding IsMyControlVisible}"></TextBlock>
    <Button Content="test" Click="Button_Click"></Button>
</Grid>
public sealed partial class MainPage : Page,INotifyPropertyChanged
{

    private Visibility _IsMyControlVisible;
    public Visibility IsMyControlVisible
    {
        get { return _IsMyControlVisible; }
        set
        {
            _IsMyControlVisible = value;
            RaisePropertyChange("IsMyControlVisible");
        }
    }

    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChange(String PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsMyControlVisible = IsMyControlVisible == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
    }
}

Please note that you need to implement the INotifyPropertyChanged interface, when the property value is change, it will notify the UI.