2

I have a partial class code behind for a XAML form. Here is the code behind:

partial class GalleryDictionary
{
    private void GalleryItemLabelPropertyChanged(object sender, RoutedEventArgs e)
    {
        var mainWindow = Application.Current.MainWindow as MainWindow;
        if (mainWindow == null)
            return; 

        var tb = sender as TextBox;
        if (tb == null)
            return;

        var sip = tb.DataContext as StereoImagePair;
        if (sip == null)
            return;

        sip.Label = tb.Text;

        mainWindow.Db.SubmitChanges();
    }
}

That is the entire DOC.XAML.CS file after the namespace declaration.

In my XAML file I need to access a variable from this XAML.CS file that I'd like to call DriveOnRight. Here is the line I've been fighting:

        <ToggleButton Grid.Row="1" Grid.Column="2" Width="26" Height="26" IsChecked="True" Click="GalleryItemLabelPropertyChanged" CommandParameter="2" Name="PS" Content="{Binding DriveOnRight}"></ToggleButton>

Where the XAML file reads....Content="{Binding DriveOnRight}" is where I need to access the variable from the CS file and display it there.

How do I do that?

Any help sure will be appreciated.

4

3 回答 3

1

看到这是在您的代码中,为了与您的方法保持一致,您可以使用依赖属性作为您的值。

public string DriveOnRight
{
    get { return (string)GetValue(DriveOnRightProperty); }
    set { SetValue(DriveOnRightProperty, value); }
}
public static readonly DependencyProperty DriveOnRightProperty =
    DependencyProperty.Register("DriveOnRight", typeof(string), typeof(GalleryDictionary));

尽管我建议 Krish 的设计使用 ViewModel 会更好。

于 2013-06-24T13:08:35.977 回答
0

您可以在后面的代码中分配一个值,该值与按钮或切换按钮的 LOADED 函数或使用它的任何对象相关联。

在您的函数上方设置值的级别,制作这样的代码。

    public static string DriveOnRight = "True";  
    //"True" could be a condition or function or whatever.

然后,它可能在后面的代码中如下所示:

    private void DS_Loaded(object sender, RoutedEventArgs e)
    {
        ToggleButton tg = sender as ToggleButton;
        if (//WhateverYourPathToTheVariable//.DriveOnRight == "True")
        {
            tg.Content = "DS"; 
        }
        else
        {
            tg.Content = "PS";
        }

    }

然后......在 XAML 代码中,你有这个:

        <ToggleButton Loaded="DS_Loaded" Grid.Row="1" Grid.Column="0" Width="26" Height="26" Name="DS" Content="DS"></ToggleButton> 
于 2013-07-15T22:33:31.463 回答
0

这是一种方法:

public class GalleryDictionaryViewmodel: INotifyPropertyChanged
{
     private string _DriveOnRight;
     public string DriveOnRight
     {
         get{ return _DriveOnRight;}
         set{ _DriveOnRight = value;}
     }
}

在您的 CodeBehind 文件 (DOC.XAML.CS) 中:

partial class GalleryDictionary
{
      public GalleryDictionaryViewModel GDViewModel;
      public GalleryDictionary()
      {
          GDViewMOdel = new GDViewModel();
          GDViewModel.DriveOnRight = "";
          this.DataContext = GDViewModel;
      }
}

在您的 Xaml 文件中:

 <ToggleButton Grid.Row="1" Grid.Column="2" Width="26" Height="26" IsChecked="True" Click="GalleryItemLabelPropertyChanged" CommandParameter="2" Name="PS" Content="{Binding DriveOnRight}"></ToggleButton>

希望这会帮助你。

于 2013-06-24T12:51:50.663 回答