[英]Passing value from bound ViewModel to UserControl
I am trying to create a conditional textbox usercontrol on which there will be a property that accepts 我试图创建一个条件文本框用户控件,在该控件上将有一个接受属性
The problem is my StaticResource and literal value is working good except for the binding. 问题是我的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>
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 结果,StaticResource和文字值成功到达UserControl,也许我错过了它不会影响的绑定
Result 结果
Any help would be appreciated. 任何帮助,将不胜感激。
TIA TIA
I would start by looking at the datacontext resolution. 我将从查看datacontext分辨率开始。 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. 如果运行程序并查看Visual Studio调试输出,您将获得一些调试行,这些行描述了WPF解决绑定的尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.