相关疑难解决方法(0)

WPF绑定到变量/ DependencyProperty

我正在玩WPF Binding和变量.显然,只能绑定DependencyProperties.我提出了以下内容,它完全正常:代码隐藏文件:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public string Test
    {
        get { return (string)this.GetValue(TestProperty); }
        set { this.SetValue(TestProperty, value); }
        //set { this.SetValue(TestProperty, "BBB"); }
    }
    public static readonly DependencyProperty TestProperty = DependencyProperty.Register(
      "Test", typeof(string), typeof(MainWindow), new PropertyMetadata("CCC"));

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Test);
        Test = "AAA";
        MessageBox.Show(Test);
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <TextBox Height="31" HorizontalAlignment="Left" Margin="84,86,0,0" Name="textBox1" VerticalAlignment="Top" Width="152" …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml binding dependency-properties

6
推荐指数
1
解决办法
1万
查看次数

WPF:XAML属性声明没有通过Setters设置?

我有一个WPF应用程序,我在代码隐藏中使用依赖属性,我想通过XAML声明设置.

例如

<l:SelectControl StateType="A" Text="Hello"/>
Run Code Online (Sandbox Code Playgroud)

所以在这个例子中我有一个UserControl被调用的SelectControl,它有一个属性StateType,在它的setter中操作一些其他的属性.

为了帮助说明问题,我Text在示例中调用了另一个属性,继续阅读,我将进一步解释.

Codebehind摘录......

public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SelectControl));

public String Text
{
  get { return (String)GetValue(TextProperty); }
  set { SetValue(TextProperty, value); }
}

public static readonly DependencyProperty StateTypeProperty = DependencyProperty.Register("StateType", typeof(String), typeof(SelectControl));

public String StateType
{
  get { return (String)GetValue(StateTypeProperty) }
  set
    {
      switch (value)
      {
        case "A":
          AnotherPropertyBoolean = true;
          break;
        case "B":
          AnotherPropertyBoolean = false;
          break;
       default:
         // this is only …
Run Code Online (Sandbox Code Playgroud)

wpf setter xaml dependency-properties

5
推荐指数
1
解决办法
2805
查看次数

标签 统计

dependency-properties ×2

wpf ×2

xaml ×2

binding ×1

c# ×1

setter ×1