Tho*_*que 72
您必须使绑定双向:
<checkbox IsChecked="{Binding Path=MyProperty, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
Car*_*rlo 50
你需要一个依赖属性:
public BindingList<User> Users
{
get { return (BindingList<User>)GetValue(UsersProperty); }
set { SetValue(UsersProperty, value); }
}
public static readonly DependencyProperty UsersProperty =
DependencyProperty.Register("Users", typeof(BindingList<User>),
typeof(OptionsDialog));
Run Code Online (Sandbox Code Playgroud)
完成后,将复选框绑定到依赖项属性:
<CheckBox x:Name="myCheckBox"
IsChecked="{Binding ElementName=window1, Path=CheckBoxIsChecked}" />
Run Code Online (Sandbox Code Playgroud)
为此,您必须在其openning标记中命名Window或UserControl,并在ElementName参数中使用该名称.
使用此代码,每当您更改代码端的属性时,您将更改文本框.此外,每当您选中/取消选中文本框时,依赖项属性也会更改.
编辑:
创建依赖项属性的一种简单方法是键入代码段propdp,它将为您提供依赖项属性的常规代码.
所有代码:
XAML:
<Window x:Class="StackOverflowTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" x:Name="window1" Height="300" Width="300">
<Grid>
<StackPanel Orientation="Vertical">
<CheckBox Margin="10"
x:Name="myCheckBox"
IsChecked="{Binding ElementName=window1, Path=IsCheckBoxChecked}">
Bound CheckBox
</CheckBox>
<Label Content="{Binding ElementName=window1, Path=IsCheckBoxChecked}"
ContentStringFormat="Is checkbox checked? {0}" />
</StackPanel>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
C#:
using System.Windows;
namespace StackOverflowTests
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public bool IsCheckBoxChecked
{
get { return (bool)GetValue(IsCheckBoxCheckedProperty); }
set { SetValue(IsCheckBoxCheckedProperty, value); }
}
// Using a DependencyProperty as the backing store for
//IsCheckBoxChecked. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsCheckBoxCheckedProperty =
DependencyProperty.Register("IsCheckBoxChecked", typeof(bool),
typeof(Window1), new UIPropertyMetadata(false));
public Window1()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意后面唯一的代码是Dependency Property.标签和复选框都绑定到它.如果复选框发生更改,则标签也会更改.
小智 11
您好,这是我第一次发帖,请耐心等待:我的回答是创建一个简单的属性:
public bool Checked { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后设置Checkbox的数据上下文(称为cb1):
cb1.DataContext = this;
Run Code Online (Sandbox Code Playgroud)
然后在xaml中绑定它的IsChecked proerty
IsChecked="{Binding Checked}"
Run Code Online (Sandbox Code Playgroud)
代码是这样的:
XAML
<CheckBox x:Name="cb1"
HorizontalAlignment="Left"
Margin="439,81,0,0"
VerticalAlignment="Top"
Height="35" Width="96"
IsChecked="{Binding Checked}"/>
Run Code Online (Sandbox Code Playgroud)
代码背后
public partial class MainWindow : Window
{
public bool Checked { get; set; }
public MainWindow()
{
InitializeComponent();
cb1.DataContext = this;
}
private void myyButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(Checked.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
应该比那更容易。只需使用:
<Checkbox IsChecked="{Binding Path=myVar, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
192975 次 |
| 最近记录: |