【问题标题】:Passing value from bound ViewModel to UserControl将值从绑定的 ViewModel 传递给 UserControl
【发布时间】:2016-07-07 13:26:11
【问题描述】:

我正在尝试创建一个条件文本框用户控件,在该用户控件上会有一个接受的属性

  • 条件(真|假,绑定)
  • 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)"
            };
        }
    }

还有这个虚拟机

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 和文字值成功到达 UserControl,也许我错过了一些不会影响的绑定

结果

任何帮助将不胜感激。

TIA

【问题讨论】:

    标签: c# xaml winrt-xaml


    【解决方案1】:

    我将从查看数据上下文分辨率开始。您是否尝试过,只是为了确保数据上下文正确:

    <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 尝试解析绑定的调试行。

    【讨论】:

    • 感谢您的回复。我找不到 PresentationTraceSources。 PS:这是windows store应用而不是wpf
    • 我认为第一个建议不起作用?查看wpftutorial.net/DebugDataBinding.html,了解如何通过显式指定命名空间来使用 PresentationTraceSources。
    猜你喜欢
    • 1970-01-01
    • 2021-06-08
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多