1

ラベル ボタンに新しい問題が発生しました。以下のコードは、ビューをビュー モデルにバインドします。

<Label Name="isImageValid"  Content="Image not Created" Margin="0,7,1,0" Style="{StaticResource LabelField}"
                Grid.ColumnSpan="2" Grid.Row="15" Width="119" Height="28" Grid.RowSpan="2"
                Grid.Column="1" IsEnabled="True" 
                Visibility="{Binding isImageValid}" />

そして、以下は私のViewModelからのコードです:

 private System.Windows.Visibility _isImageValid;
 public System.Windows.Visibility isImageValid
        {

            get
            {

                return _isImageValid;
            }
            set
            {


                _isImageValid = value;
               this.RaisePropertyChanged(() => this.isImageValid);

            }
        }
  private void OnImageResizeCompleted(bool isSuccessful)
    {

        if (isSuccessful)
        {

            this.SelectedStory.KeyframeImages = true;
            isImageValid = System.Windows.Visibility.Visible;
        }
        else
        {
            this.SelectedStory.KeyframeImages = false;

        }
    }

ラベルは、「OnImageResizeCompleted」が呼び出されるまで非表示のままにすることを意図していますが、何らかの理由で画像が常に表示されています。非表示にするには、何を変更する必要がありますか?

4

2 に答える 2

3

あなたの問題は実際のバインディング モードではありません。ラベルは通常、ソースを設定しないため、双方向バインディングは必要ありません。

@blindmeis が提案したように、viewmodel から Visibility 値を直接返すのではなく、コンバーターを使用する必要があります。使用できるフレームワークに組み込まれているものがあります。また、datacontext が正しく設定されていることを確認する必要があります。正しく設定されていない場合、ラベルは指定されたプロパティにバインドできません。ビューモデルに正しくバインドされている同じウィンドウに他のアイテムがありますか? また、バインディング エラーがないか出力ウィンドウを確認する必要があります。エラーはそこに記載されています。最後に、プロパティが正しく通知していることも確認する必要があります。これは、提供したコードからはわかりません。

コントロール/ウィンドウは次のようになります

<UserControl    x:Class="..."
                x:Name="MyControl"

                xmlns:sysControls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"

                >   
    <UserControl.Resources> 
        <sysControls:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>

    <Grid>
        <Label  Visibility="{Binding IsImageValid, Converter={StaticResource BooleanToVisibilityConverter}}" />
    </Grid>
</UserControl>

そしてC#:

public class MyViewModel : INotifyPropertyChanged
{

    public bool IsImageValid 
    {
        get { return _isImageValid; }
        set 
        {
            _isImageValid = value;
            OnPropertyChanged("IsImageValid");
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private bool _isImageValid;
}
于 2012-05-09T07:32:49.337 に答える
0

バインディングモードを双方向に設定してみてください

<Label  Visibility="{Binding isImageValid, Mode=TwoWay}" />

それにもかかわらず、私はビューモデルで System.Windows 名前空間を使用しません。bool プロパティを作成し、バインディングで booltovisibility コンバーターを使用します。

于 2012-05-09T07:05:01.433 に答える