私はC#を学び、整数を読み込んでXML設定ファイルに書き込むUIを構築しています。 UIは、さまざまなカスタムユーザーコントロールを使用します。私は1つのint変数(コントロールは0,1,2を返します)にバインドする3 radiobuttonユーザーコントロールを持っています。コントロールはイベントを使用して更新をトリガーします。 3つのisCheckedプロパティを調べて、新しいint値を決定します。しかし、元のバインド値をコードの背後から更新する方法はわかりません。メインウィンドウに1つ、ユーザーコントロールに2つのバインドが存在するため、これは一度削除されました。初心者がこの時点で迷っているので。 3つのラジオボタンにint値を読み込むBTWは、コンバーターを使って作業しています。ここでC#コードの背後にあるWPFバインディングの値を更新できますか?
は
namespace btsRV7config.controls
{
public partial class ui3X : UserControl
{
public ui3X()
{
InitializeComponent();
}
void _event(object sender, RoutedEventArgs e)
{
int newValue = 0;
if (rdbttn1.IsChecked == true) { newValue = 0; }
else if (rdbttn2.IsChecked == true) { newValue = 1; }
else if (rdbttn3.IsChecked == true) { newValue = 2; }
txtb.Text = newValue.ToString(); //tempRemove
// !!! assign newValue to Binding Source !!!
//---------------------------------------------
uiBinding1 = newValue;
BindingOperations.GetBindingExpression(rdbttn1, RadioButton.IsCheckedProperty).UpdateSource();
//---------------------------------------------
}
public static readonly DependencyProperty uiBinding1Property = DependencyProperty.Register("uiBinding1", typeof(int), typeof(ui3X));
public int uiBinding1
{
get { return (int)GetValue(uiBinding1Property); }
set { SetValue(uiBinding1Property, value); }
}
public static readonly DependencyProperty uiBinding2Property = DependencyProperty.Register("uiBinding2", typeof(int), typeof(ui3X));
public int uiBinding2
{
get { return (int)GetValue(uiBinding2Property); }
set { SetValue(uiBinding2Property, value); }
}
public static readonly DependencyProperty uiBinding3Property = DependencyProperty.Register("uiBinding3", typeof(int), typeof(ui3X));
public int uiBinding3
{
get { return (int)GetValue(uiBinding3Property); }
set { SetValue(uiBinding3Property, value); }
}
}
}
、ここでユーザ制御XAMLここ
<UserControl x:Class="btsRV7config.controls.ui3X"
x:Name="root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal" Height="22">
<RadioButton Name="rdbttn1" VerticalAlignment="Center" Margin="0 0 10 0"
IsChecked="{Binding ElementName=root, Path=uiBinding1}"
Click="_event" />
<RadioButton Name="rdbttn2" VerticalAlignment="Center" Margin="0 0 10 0"
IsChecked="{Binding ElementName=root, Path=uiBinding2}"
Click="_event" />
<RadioButton Name="rdbttn3" VerticalAlignment="Center"
IsChecked="{Binding ElementName=root, Path=uiBinding3}"
Click="_event" />
<TextBox Name="txtb" Margin="5 0 0 0" Width="20" Height="17" /> <!-- tempRemove -->
</StackPanel>
</UserControl>
がMainWindow.xaml
で使用されるユーザ制御の一例である...ユーザ制御xaml.csあります<Window x:Class="btsRV7config.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:btsRV7config.controls"
xmlns:converter="clr-namespace:btsRV7config.converters"
Title="Vans RV7 Configuration" Height="350" Width="525" >
<Window.Resources>
<converter:Int_Int_Bool_Converter x:Key="Int_Int_Bool" />
</Window.Resources>
<Grid>
<controls:ui3X uiName="Font Color" ui1="Black" ui2="Green" ui3="Cyan"
uiBinding1="{Binding RV7sld_DFfontColor, Converter={StaticResource Int_Int_Bool}, ConverterParameter=0}"
uiBinding2="{Binding RV7sld_DFfontColor, Converter={StaticResource Int_Int_Bool}, ConverterParameter=1}"
uiBinding3="{Binding RV7sld_DFfontColor, Converter={StaticResource Int_Int_Bool}, ConverterParameter=2}" />
</Grid>
</Window>
namespace btsRV7config
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
record data = new record();
DataContext = data;
}
}
public class record : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _RV7sld_DFfontColor = RV7sld_dict["DFfontColor"];
public int RV7sld_DFfontColor
{
get
{ return _RV7sld_DFfontColor; }
set
{
_RV7sld_DFfontColor = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("RV7sld_DFfontColor"));
}
}
}
}
}
非常に多くのコードを投稿して申し訳ありません - 重要なのは、ユーザーがxaml.csを一番上に置いていることです。
ここにUIの画像へのリンクがあります。 投稿したコードを簡略化しました。 http://www.baytower.ca/photo/uiSample.jpg
だから、 - 'フォントの色'(RV7sld_DFfontColor)は黒にすることができます(0)緑(1)シアン(2)
ダニー
別のラジオボタンがチェックされているときにバインディング 'RV7sld_DFfontColor'の値を更新しようとしていますか? –
また、達成しようとしていることについては、 'RadioButton.GroupName'依存プロパティを調べることをお勧めします。 –
はい、radioButtonをクリックすると、 "RV7sld_DFfontColor"値を "newValue"に更新したいと思います。 ...また、ラジオボタンがユーザーコントロール内にあるため、既に適切にグループ化されているため、見つけました。グループを割り当てたとき、ユーザーコントロールの各インスタンスは同じグループを使用していました。 –