gpt4 book ai didi

wpf - 如何绑定(bind)文本框和属性?

转载 作者:行者123 更新时间:2023-12-02 00:58:20 24 4
gpt4 key购买 nike

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Toolkit"
x:Class="SilverlightApplication5.MainPage"
Width="640" Height="480">
<StackPanel x:Name="LayoutRoot" Background="White">
<TextBox x:Name="tbWidth" TextWrapping="Wrap"
Text="{Binding Mode=TwoWay, ValidatesOnExceptions=True, Path=RoomWidth}"/>
</StackPanel>
</UserControl>

RoomWidth - 是属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication5
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private int roomWidth = 10;
public int RoomWidth
{
get { return roomWidth; }
set
{
if (value < 0 || value > 100)
{
throw new Exception("Data not correct");
}
roomWidth = value;
}
}

}
}

我需要将此类添加到绑定(bind)源。这是怎么做的?

最佳答案

使用 ElementName 例如:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Toolkit"
x:Class="SilverlightApplication5.MainPage"
Width="640" Height="480"
Name="control">

<!-- ... -->
<TextBox Text="{Binding ElementName=control, Mode=TwoWay, ValidatesOnExceptions=True, Path=RoomWidth}" x:Name="tbWidth" TextWrapping="Wrap"/>

如果您对基本绑定(bind)有疑问,那么您应该阅读它。 ( WPF/Silverlight )

关于wpf - 如何绑定(bind)文本框和属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6158662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com