将值从绑定的ViewModel传递到UserControl

时间:2016-03-21 06:10:06

标签: c# xaml winrt-xaml

我正在尝试创建一个条件文本框usercontrol,其中将有一个接受

的属性
  • 条件(true | false,Binding)
  • FalseValue(true | false,Binding,StaticResource)
  • TrueValue(true | false,Binding,StaticResource)

问题是我的 StaticResource和文字值除了绑定外还能正常工作。

<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>

代码隐藏

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

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

和这个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"));
            }
        }
    }

结果,StaticResource和literal值成功到达UserControl,也许我错过了它不会影响的绑定

结果

enter image description here

任何帮助都将不胜感激。

TIA

1 个答案:

答案 0 :(得分:0)

我首先看一下datacontext分辨率。您是否尝试过,只是为了确保datacontext正确无误:

<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>

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)"
        };
    }
}

检查绑定跟踪信息可能也有帮助。试试

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

如果您运行程序并查看visual studio调试输出,您将获得一些描述WPF试图解析绑定的调试行。