1

私は次のxamlを持っています:

<Window x:Class="Retail_Utilities.Dialogs.AdjustPriceDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ShowInTaskbar="False"
    WindowStartupLocation="CenterOwner" Name="Adjust_Price"
    Title="Adjust Price" Background="#ee0e1c64" AllowsTransparency="True" WindowStyle="None" Height="330" Width="570" KeyDown="Window_KeyDown" Loaded="Window_Loaded">

<Grid Height="300" Width="550">
<ListBox HorizontalAlignment="Right" Margin="0,110,35,60" Name="lstReasons" Width="120" VerticalAlignment="Stretch"
             ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}, Path=reasons}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=POS_Price_Change_Reason}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
</Window>

関連するC#は次のとおりです。

namespace Retail_Utilities.Dialogs
{
public partial class AdjustPriceDialog : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Twr_POS_Price_Change_Reason> reasons; ...

最後に、このウィンドウを開く別のページのコードを次に示します。

AdjustPriceDialog apd = new AdjustPriceDialog();
apd.Owner = (Window)this.Parent;
apd.reasons = new ObservableCollection<Twr_POS_Price_Change_Reason>();
var pcr = from pc in ctx.Twr_POS_Price_Change_Reasons where pc.Deactivated_On == null select pc;
foreach (Twr_POS_Price_Change_Reason pc in pcr)
{
    apd.reasons.Add(pc);
}
apd.AdjustingDetail = (Twr_POS_Invoice_Detail)lstDetails.SelectedItem;
if (apd.ShowDialog() == true)
{

}

ダイアログボックスが開くと、lstReasonsリストは空です。エラーは発生しません。コードを停止すると、reasonsコレクションにテーブルのアイテムが入力されていることがわかります。

4

3 に答える 3

0

問題は、プロパティをどのように作成しているかにあるようです。私はあなたがあなたの財産を観察可能なコレクションとして置いていることを知っていますが、これはそれがそれ自体で観察可能であるという意味ではありません!したがって、このプロパティが変更された場合は、セッターで次のような操作を行ってUIに通知する必要があります。

public ObservableCollection<Twr_POS_Price_Change_Reason> reasons
{
get{....}
set
{
Notify('reasons')
}
}

しばらくWPFを使用していなかったため、正確なコードを覚えていませんが、これはINotifyPropertyChangedのメソッドです。頑張ってください。

于 2012-08-03T16:22:05.463 に答える
0

理由はプロパティである必要があります(追加{ get; set;})。また、VisualStudioの出力を確認してください。バインドエラーが表示されます。理由へのバインドの失敗に関する情報があるはずです。

于 2012-08-03T16:17:43.917 に答える
0

POS_Price_Change_Reasonプロパティの名前が。であるのに対し、バインディングパスはに設定されているようですreasonsPOS_Price_Change_Reasonサンプルコードに含めず、reasonsこのプロパティのバッキングフィールドである場合を除きます。

また、バインドできるのはパブリックプロパティのみであり、フィールドにはバインドできないことに注意してください。さらに、プロパティの値を変更する場合は、そのプロパティのイベントを呼び出して、この変更をビューに通知する必要があります。PropertyChangedEventHandler

PropertyChanged(new PropertyChangedEventArgs("YourPropertyName"));
于 2012-08-03T16:21:11.723 に答える