我不認爲你的代碼有任何問題。它似乎在我的最後工作完美。我已經嘗試了xaml和codebehind,以及完全在代碼背後。
兩者均按預期工作,裝訂完美!
我在下面包含了一個示例。覈實。綁定在我的最後完美無缺!
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
>
<Grid x:Name="Grid1" />
</Window>
代碼背後:
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ToggleButton two = new ToggleButton();
two.Content = "Two";
two.Width = 100;
two.Height = 50;
/* Set up ToggleButton here*/
this.Grid1.Children.Add(two);
ToggleButton one = new ToggleButton();
if (one == null) return;
one.Content = "One";
one.Width = 100;
one.Height = 50;
one.Margin = new Thickness(0, 0, 250, 0);
this.Grid1.Children.Add(one);
Binding binding = new Binding("IsChecked");
binding.Source = two;
binding.Mode = BindingMode.TwoWay;
one.SetBinding(ToggleButton.IsCheckedProperty, binding);
/* Add two to the UI */
}
}
}
不要在代碼中創建綁定,這是一個爛攤子。 –
所以你想切換按鈕做正常切換按鈕做的相反嗎?爲什麼你需要一對按鈕是相同的? – Paparazzi
@Paparazzi他們代表服務器上的同一個對象,但在不同的菜單中 – KevinA