1
votes

I am trying to create a conditional textbox usercontrol on which there will be a property that accepts

  • Condition (true|false, Binding)
  • FalseValue (true|false, Binding, StaticResource)
  • TrueValue (true|false, Binding, StaticResource)

The problem is my StaticResource and literal value is working good except for the binding.

<Grid Background="#FFBDBDBD" Margin="0,154,0,266">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="33*"></RowDefinition>
        <RowDefinition Height="33*"></RowDefinition>
        <RowDefinition Height="33*"></RowDefinition>
    </Grid.RowDefinitions>
    <userControls:ConditionalTextBox 
        Grid.Column="0"
        Grid.Row="0"
        Condition="{Binding Path=IsTrue, Mode=TwoWay}"
        FalseValue="{StaticResource FalseValRes}" 
        TrueValue="{StaticResource TrueValRes}" 
        HorizontalAlignment="Left" Width="500">
    </userControls:ConditionalTextBox>

    <userControls:ConditionalTextBox 
        Grid.Column="0"
        Grid.Row="1"
        Condition="True"
        FalseValue="{Binding FalseValueDefined}" 
        TrueValue="{Binding TrueValueDefined}" 
        HorizontalAlignment="Left" Width="500">
    </userControls:ConditionalTextBox>

    <userControls:ConditionalTextBox 
        Grid.Column="0"
        Grid.Row="2"
        Condition="False"
        FalseValue="False Value (string)" 
        TrueValue="True Value (string)" 
        HorizontalAlignment="Left" Width="500">
    </userControls:ConditionalTextBox>
</Grid>

with code behind

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.DataContext = new VMTest
            {
                IsTrue = true,
                FalseValueDefined = "False Value (Binding)",
                TrueValueDefined = "True Value (Binding)"
            };
        }
    }

and this VM

public class VMTest : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        private bool isTrue;
        public bool IsTrue
        {
            get { return isTrue; }
            set
            {
                isTrue = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsTrue"));
            }
        }

        private string trueValueDefined;
        public string TrueValueDefined
        {
            get { return trueValueDefined; }
            set
            {
                trueValueDefined = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("TrueValueDefined"));
            }
        }

        public string falseValueDefined;
        public string FalseValueDefined
        {
            get { return falseValueDefined; }
            set
            {
                falseValueDefined = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("FalseValueDefined"));
            }
        }
    }

as the result, StaticResource and literal value reached successfully to UserControl and maybe i missed something on bindings that it wont affect

Result

enter image description here

Any help would be appreciated.

TIA

1

1 Answers

0
votes

I would start by looking at the datacontext resolution. Have you tried, just to make sure the datacontext is correct:

<userControls:ConditionalTextBox 
    x:Name="boundControl"
    Grid.Column="0"
    Grid.Row="1"
    Condition="True"
    FalseValue="{Binding FalseValueDefined}" 
    TrueValue="{Binding TrueValueDefined}" 
    HorizontalAlignment="Left" Width="500">
</userControls:ConditionalTextBox>

and

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        this.boundControl.DataContext = new VMTest
        {
            IsTrue = true,
            FalseValueDefined = "False Value (Binding)",
            TrueValueDefined = "True Value (Binding)"
        };
    }
}

It might also be helpful to examine the binding trace information. Try

 "{Binding FalseValueDefined, PresentationTraceSources.TraceLevel=High}"

If you run your program and look at the visual studio debug output, you will get some debug rows describing WPF's attempts to resolve the binding.