1
ボタンを作成しようとしています。リストボックスに少なくとも1つの項目がある場合にのみ、有効にします。 AutoCompleteBoxリストからリストボックス項目を選択して追加します。リストボックスにisEnabledプロパティをバインドしようとしました。リストボックスにコンテンツがあるかどうかを確認するためにBoolConverterを使用しました。設定ボタンのプロパティリストボックスにSilverlightのBoolConverterでコンテンツがある場合、IsEnabledに基づいていますか?
<Button x:Name="DisabledButton" Click="RemoveButton_Click" Content="Disabled Button" IsEnabled="{Binding ItemsSource, ElementName=EntitiesListBox,Converter={StaticResource CountToBooleanConverter}}" />
何かが不足しています。誰かが私に何が間違っているか教えてもらえないかと思います。どんなアイデアも高く評価されています!
SilverlightのXAML:背後
<UserControl.Resources>
<local:BoolToOppositeBoolConverter x:Key="CountToBooleanConverter" />
<local:CountGreaterThanZeroConverter x:Key="CountGreaterThanZeroConverter" />
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="White" Width="150">
<TextBlock Text="{Binding ElementName=MyAutoCompleteBox, Path=SelectedItem, TargetNullValue='No item selected', StringFormat='Selected Item: {0}'}" />
<sdk:AutoCompleteBox x:Name="MyAutoCompleteBox" IsTextCompletionEnabled="True" ItemsSource="{Binding Items}" />
<Button x:Name="AddButton" Click="AddButton_Click" Content="AddButton" />
<Button x:Name="RemoveButton" Click="RemoveButton_Click" Content="RemoveButton" />
<Button x:Name="DisabledButton" Click="RemoveButton_Click" Content="Disabled Button" IsEnabled="{Binding ItemsSource, ElementName=ListBox,Converter={StaticResource CountToBooleanConverter}}" />
<Button x:Name="DisabledButton2" Click="RemoveButton_Click" Content="Disabled Button" IsEnabled="{Binding ItemsSource.Count, ElementName=ListBox, Converter={StaticResource CountGreaterThanZeroConverter}}" />
</StackPanel>
コード:
public partial class MainPage : UserControl
{
string currentItemText;
public ObservableCollection<string> Items
{
get;
private set;
}
public MainPage()
{
InitializeComponent();
Items = new ObservableCollection<string>();
Items.Add("One");
Items.Add("Two");
Items.Add("Three");
Items.Add("Four");
DataContext = this;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
currentItemText = MyAutoCompleteBox.SelectedItem.ToString();
ListBox.Items.Add(currentItemText);
ApplyDataBinding();
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
if (ListBox.SelectedItems != null)
{
int count = ListBox.SelectedItems.Count - 1;
for (int i = count; i >= 0; i--)
{
ListBox.Items.Remove(ListBox.SelectedItems[i]);
}
ApplyDataBinding();
}
}
private void ApplyDataBinding()
{
MyAutoCompleteBox.ItemsSource = null;
}
}
public class CountGreaterThanZeroConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)value > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
提案をいただき、ありがとうございます。私はそれを実装しようとしたが、ここでObservableCollectionを設定するのに問題がある。上記のコードを新しいコンバータで更新しました。私はあなたがエラーのコードをチェックするwoudn't場合は、私は思っています。前もって感謝します。 – vladc77
アイテム=新しいリスト(); => Items =新しいObservableCollection (); Observableコレクションは、コレクションに変更があったときに通知を送信するコレクションであり、データバインディングには理想的です。 –
私は観察可能なコレクションを初めて使用しています。それは少し混乱しています。私はあなたが何を意味したのかよくわからない 'Items = new List(); => Items =新しいObservableCollection (); 'つまり、このコレクションを割り当てるためにコード内で何を変更する必要があるのか分かりません。 –
vladc77