C# 将类中的属性直接绑定到 XAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14190479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding a property from a class to XAML directly
提问by djangojazz
I was curious as I was learning more on binding with WPF do you HAVE TO set the data context to simply set the {binding path=} of a single property? I was just curious as I was learning from the MVVM example of code behind and it appears I have a situation I want to bind to something not in the data context of the ViewModel and I would prefer an intermediary class over code behind to reference in a binding. So could I have yet another class I reference for either a second datacontext or something similar? Or could I set a resource key and reference the class somehow? I ultimately want to access a property in a separate class if possible.
我很好奇,因为我正在学习有关 WPF 绑定的更多信息,您是否必须设置数据上下文以简单地设置单个属性的 {binding path=}?我只是好奇,因为我正在从背后代码的 MVVM 示例中学习,看来我有一种情况我想绑定到不在 ViewModel 的数据上下文中的东西,我更喜欢中间类而不是代码背后的引用捆绑。那么我可以为第二个数据上下文或类似的东西引用另一个类吗?或者我可以设置一个资源键并以某种方式引用该类吗?如果可能的话,我最终想访问一个单独的类中的属性。
EG:
例如:
<Window x:Class="WPFTestBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:da="clr-namespace:WPFTestBinding.DataAccess"
xmlns:main="clr-namespace:WPFTestBinding"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Label Content="Here we go: "/>
<TextBox x:Name="testtext" />
<Label Height="50" />
<!-- CODE BELOW WILL NOT WORK -->
<TextBox Text="{Binding Path=TestID}" x:Name="testtext2" />
</StackPanel>
</Grid>
</Window>
I know I can set the value of a TextBox in code behind and it works in the example of a property but it will not for the binding. Is there a way to do simple binding on the fly for properties from classes? I have not found a simple example that does this and I was searching online and while learning binding most examples are either very intense sets of settings data contexts or very simple static resource examples. I was just curious if you could use the {Binding Path=} and extend some other property on the fly to just get the code in the class.
我知道我可以在后面的代码中设置 TextBox 的值,它在属性示例中有效,但不适用于绑定。有没有办法动态地对类的属性进行简单的绑定?我还没有找到一个简单的例子,我在网上搜索,在学习绑定时,大多数例子要么是非常密集的设置数据上下文集,要么是非常简单的静态资源示例。我只是好奇您是否可以使用 {Binding Path=} 并动态扩展一些其他属性来获取类中的代码。
Rest of code is pretty simple:
其余代码非常简单:
namespace WPFTestBinding.DataAccess
{
class Test
{
public string TestID { get { return "This is my test"; } }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataAccess.Test t = new Test();
testtext.Text = t.TestID; // code behind works easily
}
}
采纳答案by kusi581
You can set the DataContext in Xaml like this:
您可以像这样在 Xaml 中设置 DataContext:
<Window xmlns:da="clr-namespace:WPFTestBinding.DataAccess">
<Window.DataContext>
<da:Test/>
<Window.DataContext>
<TextBox Text="{Binding TestID}"/>
</Window>
回答by Sergii Vashchyshchuk
The data context is not set. The DataBinding doesn't know where to take TestID from. Here is correct code behind:
未设置数据上下文。DataBinding 不知道从哪里获取 TestID。下面是正确的代码:
namespace WPFTestBinding.DataAccess
{
class Test
{
public string TestID { get { return "This is my test"; } }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataAccess.Test t = new Test();
DataContext = t;
}
}
回答by Harsh Baid
Some points to note:
需要注意的几点:
- The property
TestID
you are trying to bind is read-only, as it only has get-accessor. Therefore, Binding should beOneWay
only. - Assigning the DataContext: You can assign the instance holding your model such MainViewModel with ICollection<BaseViewModel> property (which would be having all the derived instances in the collection) or directly the model itself (as in your case). As I have done in code below.
TestID
您尝试绑定的属性是只读的,因为它只有 get-accessor。因此,Binding 应该是OneWay
唯一的。- 分配 DataContext:您可以分配包含模型的实例,例如带有 ICollection<BaseViewModel> 属性的 MainViewModel(它将包含集合中的所有派生实例)或直接分配模型本身(如您的情况)。正如我在下面的代码中所做的那样。
Code
代码
namespace WPFTestBinding.DataAccess
{
class Test
{
public string TestID { get { return "This is my test"; } }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataAccess.Test testInstance = new Test();
this.DataContext = testInstance;
}
}
XAML
XAML
<TextBox Text="{Binding Path=TestID, Mode=OneWay}" x:Name="txtTestID" />
For more refer:
更多请参考:
回答by M Komaei
1 - You have to fill the Prop beforeInitializeComponent(); in .cs class.
1 - 您必须在InitializeComponent()之前填充 Prop ;在 .cs 类中。
2 - use this in Window tag : DataContext="{Binding RelativeSource={RelativeSource Self}}"
2 - 在 Window 标签中使用它:DataContext="{Binding RelativeSource={RelativeSource Self}}"
3 - Label tag : Label Content="{Binding Currency}"
3 - 标签标签:标签内容="{绑定货币}"